Convert hex strings to string, float, int and double

  • Thread starter Thread starter Uwe Kuhne
  • Start date Start date
U

Uwe Kuhne

Before I start my question, I wish all, a really happy new year. May your
wishes for 2004 come true.

Now my question:
I've an export from a proprietary old db system, which exports the data in
hexadecimal representation.

I would like to have methods like
- HexToString ("...hexadecimal string...")
- HexToInt ("...hexadecimal string...")
- HexToFloat ("...hexadecimal string...")
- HexToDouble ("...hexadecimal string...")
which converts me a hexadecimal string to the according type.

I want to give the hex string as a whole to the method e.g.
- HexToString ("3139383530323038") and not
- HexToString("\x31\x39\x38\x35\x30\x32\x30\x38")

Are there any classes in the .NET framework, which can convert hexadecimal
data in the way I would like ? If not, are there any suggestion how to
solve my problem in a different way without doing all this decoding on my
own ?
In addition I know that e.g. a float is represented according to IEEE-754
(whatever this exactly means).

Thanks for help
Uwe
 
Fairly simple really. Support is built right in

C# code:

using System.Globalization;

static long hextolong(string hex1)
{
return long.Parse(hex1, NumberStyles.AllowHexSpecifier);
}
static int hextoint(string hex1)
{
return int.Parse(hex1, NumberStyles.AllowHexSpecifier);
}
static string inttohex( int intval )
{
return intval.ToString("X");
}
static string longtohex( long longval )
{
return longval.ToString("X");
}

note: the inttohex() and longtohex() functions above will not pad your
string with leading zeros. If you want it padded, you will need to add the
leading zeros to the format specifier in the ToString() calls.

Hope this helps.

--- Nick
 
Back
Top