lcrogers
asked on
Convert hex string to double
double xx=0;
string txt=null;
txt = "0942afc8131d6837";
xx=Double.Parse(txt, System.Globalization.Numbe rStyles.He xNumber);
this complies fine but when I run the application I get this error "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll"
Is there a function in C# 2005 that will take a string Hex number and convert it to a double?
string txt=null;
txt = "0942afc8131d6837";
xx=Double.Parse(txt, System.Globalization.Numbe
this complies fine but when I run the application I get this error "An unhandled exception of type 'System.ArgumentException'
Is there a function in C# 2005 that will take a string Hex number and convert it to a double?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I need it to be a doule precession number, this will give me a whole number.
what do you mean with a double precision number? both examples comply with your question.
string txt = null;
txt = "0942afc8131d6837";
txt = Convert.ToInt64(txt, 16).ToString("#0.00");
give a string format at the end......
then convert that string to double by convert.todouble(txt );
txt = "0942afc8131d6837";
txt = Convert.ToInt64(txt, 16).ToString("#0.00");
give a string format at the end......
then convert that string to double by convert.todouble(txt );
ASKER
it must be converted to a negetive earth center to earth fix number (exsample: -53512345.3567N, -36512342.3345E; I am getting these values from a military GPS grid system.
you have to do it with unsafe code. Assuming both coordinates are inside a single hex:
unsafe public static void Transform(string hex, out float x, out float y)
{
UInt32 x0 = System.Convert.ToUInt32(he x.Substrin g(0, 8), 16); // first half of hex code
UInt32 y0 = System.Convert.ToUInt32(he x.Substrin g(8, 8), 16); // last half of hex code
x = *((float *)&x0);
y = *((float *)&y0);
}
Assumming it is just one component:
unsafe public static void Transform(string hex, out double a)
{
UInt64 a0 = System.Convert.ToUInt64(he x16);
a = *((double *)&a0);
}
but with the sample hex string you have provided, it doesn't output any valid value as you shown.
Also, you have to take care about the byte ordering (big/little endian)
unsafe public static void Transform(string hex, out float x, out float y)
{
UInt32 x0 = System.Convert.ToUInt32(he
UInt32 y0 = System.Convert.ToUInt32(he
x = *((float *)&x0);
y = *((float *)&y0);
}
Assumming it is just one component:
unsafe public static void Transform(string hex, out double a)
{
UInt64 a0 = System.Convert.ToUInt64(he
a = *((double *)&a0);
}
but with the sample hex string you have provided, it doesn't output any valid value as you shown.
Also, you have to take care about the byte ordering (big/little endian)
also you will need to instruct the compiler to allow the unsafe code in the Project Settings.
double hex2double(string hex)
{
double result = 0.0;
foreach (char c in hex)
{
double val = (double)System.Convert.ToI
result = result * 16.0 + val;
}
return result;
}