Convert hex to double in 2.0?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do you convert a hex string into a double using C# in framework v2.0?

I've seen this question answered before, but apparently something has
changed in 2.0 that negates the old solution that I've seen. The code:

DoubleVariable = Double.Parse(HexString, NumberStyles.AllowHexSpecifier);

results in the error:

"The number style AllowHexSpecifier is not supported on floating point data
types."

Thanks in advance for any help.
 
Byron,

The error is the correct result. Hexidecimal representation only
represents integral types, so you are better off using the Parse method on
Int32 (or some other integral type) and then converting that to double.

Hope this helps.
 
Byron said:
How do you convert a hex string into a double using C# in framework v2.0?

What format is the hex string in to start with? I've never seen a
double represented as a hex string. Could you give an example of what
you want?
 
An example of a hex value I've encountered is:
414BFCC90D2258ED

This is 64 bit double precision floating point representation for a
cartesian coordinate system as used in Distributed Interactive Simulation
(DIS) (from IEEE 1278.1).

http://www.sisostds.org/dis-dd/pdu/19.htm

I'm essentially trying to move an entity in a simulation and sniff network
traffic to figure out what coordinate system the application is using, so I
need to convert the hex string to a double to evaluate it. Eventually I'll
need to modify the double and translate it back into hex and put it back onto
the network to move the entity.
 
An example of a hex value I've encountered is:
414BFCC90D2258ED

This is 64 bit double precision floating point representation for a
cartesian coordinate system as used in Distributed Interactive Simulation
(DIS) (from IEEE 1278.1).

http://www.sisostds.org/dis-dd/pdu/19.htm

I'm essentially trying to move an entity in a simulation and sniff network
traffic to figure out what coordinate system the application is using, so I
need to convert the hex string to a double to evaluate it. Eventually I'll
need to modify the double and translate it back into hex and put it back onto
the network to move the entity.

You may be better off creating your own struct or class to represent
this entity and writing a method to parse the value properly.
 
I am creating a class to hold the entity. What I'm looking for is a method
to parse this one hex string into a double. The hex in the example is the
64 bit (double) Z component of the entity's current coordinate, and the X and
Y are contained in another 2 x 64 bits not illustrated.
 
Byron said:
An example of a hex value I've encountered is:
414BFCC90D2258ED

And what floating point value is that meant to represent? Just quoting
a hex number doesn't help very much without a description of either how
to interpret it or what the result of a correct interpretation would
be.

It *could* be that you read it as a long, then use
BitConverter.Int64BitsToDouble to convert to a double. But without
knowing the format, that's just a wild guess.
 
And what floating point value is that meant to represent? Just quoting
a hex number doesn't help very much without a description of either how
to interpret it or what the result of a correct interpretation would
be.

If you follow the link, it seems to explain.
 
An example of a hex value I've encountered is:
414BFCC90D2258ED

This is 64 bit double precision floating point representation for a
cartesian coordinate system as used in Distributed Interactive Simulation
(DIS) (from IEEE 1278.1).

http://www.sisostds.org/dis-dd/pdu/19.htm

Telling us what the correct interpretation of this string is would
help a lot. Also, notice that the data dictionary references doubles;
the string above is (presumably) a double encoded as a hex string.
Not the same thing.

I gave it a shot, though:

string hex = "414BFCC90D2258ED";
byte[] b = new byte[hex.Length / 2];
for (int i = (hex.Length - 2), j = 0; i >= 0; i -= 2, j++ )
{
b[j] = byte.Parse(hex.Substring(i, 2),
System.Globalization.NumberStyles.HexNumber);
}
double d = BitConverter.ToDouble(b, 0);

The value produced here is 3668370.1026107, which interpreted as
meters, is about 3700 kilometers. That sounds about right for Earth-
sized measurements. Notice that I'm walking through the string
backwards, thus treating it as little-endian (I hope I've remembered
my endian logic correctly). Treating it as big-endian gave me a very,
very small number.

Michael
 
Andy said:
If you follow the link, it seems to explain.

Does it? I saw fields which were specified to be 64-bit double
precision floating point numbers, but nothing explaining where the OP
would get a hex representation from, or what that would mean.

If I missed something (which is more than possible) please can you
point to exactly where it is? It would be nice to be able to help the
OP more...
 
Back
Top