By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,424 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

Convert hex to double in 2.0?

P: n/a
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.
Apr 6 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Byron" <By***@discussions.microsoft.comwrote in message
news:01**********************************@microsof t.com...
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.

Apr 6 '07 #2

P: n/a
Byron <By***@discussions.microsoft.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '07 #3

P: n/a
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%2Ddd/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.

"Jon Skeet [C# MVP]" wrote:
Byron <By***@discussions.microsoft.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '07 #4

P: n/a
On Apr 6, 1:10 pm, Byron <B...@discussions.microsoft.comwrote:
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%2Ddd/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.

Apr 6 '07 #5

P: n/a
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.

"Andy" wrote:
On Apr 6, 1:10 pm, Byron <B...@discussions.microsoft.comwrote:
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%2Ddd/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.

Apr 6 '07 #6

P: n/a
Byron <By***@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '07 #7

P: n/a
On Apr 6, 2:33 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
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.

Apr 6 '07 #8

P: n/a
On Apr 6, 10:10 am, Byron <B...@discussions.microsoft.comwrote:
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%2Ddd/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

Apr 6 '07 #9

P: n/a
Andy <an***@med-associates.comwrote:
On Apr 6, 2:33 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
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.
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.