Skip to content
January 16, 2008 / kiranpatils

Converting Hexadecimal Value to Int or VB6 Hexadecimal value to C# Int

Somewhat busy from last few days and can’t able to write something new here…

Today i have faced new problem i want to convert a hexadecimal value [which i have stored in string and got from VB 6.0] to Int..But i have faced so many problems as under:

1. “Could not find any recognizable digits”.

stack trace:

——————————————————————————————————–

System.FormatException was unhandled
Message=”Could not find any recognizable digits.”
Source=”mscorlib”
StackTrace:
at System.ParseNumbers.StringToInt(String s, Int32 radix, Int32 flags, Int32* currPos)
at System.Convert.ToUInt32(String value, Int32 fromBase)
at HextoInt.Program.HexToInt(String hexString) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 27
at HextoInt.Program.Main(String[] args) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

——————————————————————————————————–

Root Cause:

string Hexvalue = “&H69”; //My Hex value is creating problem Here.

Solution

I have copied this string [“&H69”] from VB 6 Output. But when you need to pass a hex value in C#. you have to keep following thing in Mind.

1. It Must starts with 0[zero]x means for my problem solution is here:

string Hexvalue = “&H69”; //ERROR PRONE CODE

string Hexvalue = “0x69”; //NO ERROR HERE

i got help from: http://www.thescripts.com/forum/thread261083.html

2. “Input string was not in a correct format.”

stack trace:

——————————————————————————————————–

System.FormatException was unhandled
Message=”Input string was not in a correct format.”
Source=”mscorlib”
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s, NumberStyles style)
at HextoInt.Program.HexToInt(String hexString) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 26
at HextoInt.Program.Main(String[] args) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
——————————————————————————————————–

Root Cause:

private static int HexToInt(string hexString)
{
return int.Parse(hexString, NumberStyles.HexNumber); // ERROR PRONE CODE
}

Solution

I have created one function for converting Hexa to Int. But it is throwing error.

Frankly speaking, I don’t know why it is not working but i tried other one and now its working..so i am providing you a working one here.

private static int HexToInt(string hexString)
{
int iHex1 = (int)System.Convert.ToUInt32(hexString, 16);
return iHex1;
}

//The above dude is working fine.

3. How to Convert Hexadecimal to Int???

I have tried so many codes,my applications and blogs and all that things which i usually do for Killing the problem. I have found this thing from so many spaces and i am putting it here for centralization purpose. and making your task easy one.

for converting Hexadecimal to Int or Vice versa use functions:

—————————————————————————————————–

1. for Hexadecimal to integer conversion

private static int HexToInt(string hexString)
{
int iHex1 = (int)System.Convert.ToUInt32(hexString, 16);
return iHex1;
}

2. for integer to Hexadecimal conversion

public static string IntToHex(int number)
{
return String.Format(“{0:x}”, number);
}

and ya don’t forget to Import System.Globalization namespace

—————————————————————————————————–

I Got Help from : http://mark.michaelis.net/Blog/HexadecimalConversionsInCNET.aspx

[OPTIONAL] And at last i am pasting my TestHarness Console application here:


using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
namespace HextoInt
{
class Program
{

static void Main(string[] args)
{

string Hexvalue = “0x69″;//”0x100000″;//”&H69”;
//Console.Write(“{0:X}”, Hexvalue);//To Print any/String value in Hexa decimal
Console.WriteLine(“HEX [which is in string] TO INT:–“+HexToInt(Hexvalue));
Console.WriteLine(“INT TO Hex[Output will be in string]:–” + IntToHex(HexToInt(Hexvalue)));

}
private static int HexToInt(string hexString)
{

int iHex1 = (int)System.Convert.ToUInt32(hexString, 16);
return iHex1;
}
public static string IntToHex(int number)
{
return String.Format(“{0:x}”, number);
}

}
}


Have a Happy Conversion!!!

One Comment

Leave a Comment

Trackbacks

  1. Kuala Lumpur Real Estate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: