473,416 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Convert hex to double in 2.0?

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
9 12069
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jeff_zhang446 | last post by:
Hi, I try to convert double to string as below: std::string cnvrtToString(double lValue) { std::ostringstream lStream; lStream << lValue; return lStream.str();
12
by: Alan | last post by:
how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I...
2
by: Pascal | last post by:
Why does this not work, and how should i do this convert in stead: string x = double.MinValue.ToString(); double y = Convert.ToDouble(x); i get this exception: An unhandled exception of type...
2
by: paul gao via .NET 247 | last post by:
hi all. In my program I need to convert a double number to stringrepresentation in fraction format and vise versa. For example,convert 0.75 to string "3/4" and convert string "1/2" to 0.5.The class...
4
by: charliewest | last post by:
Hello - I am reading up on the NumberFormatInfo class, trying to figure out how to convert a double to a percentage, using the IFormatProvider. This seems a little beyond me. Can anyone suggest...
5
by: Steven | last post by:
I have tried to use FormatNumber() to convert a very large double type number to String format, however, the result seems not correct, is there any limitation for using FormatNumber() function? ...
0
by: aparna12 | last post by:
Hi all Please tell me how to Convert double to string in fraction format (.75 to 3/4) in asp.net. Thanks Aparna
1
by: =?big5?B?qM6nuw==?= | last post by:
I already knew how to convert bytes to Double, use BitConverter.ToDouble(). but, How to convert double to bytes? for example: double a = 3,444; I want convert to bytes={ ?, ?, ?, ?, ?, ?,...
21
by: Aman JIANG | last post by:
hi I need to do this (convert double to string) fast, safe and portable. Is there any way to do this ? Except the ways following: 1. C++ I/O stream, stringstream (and boost::lexical_cast) 2....
1
by: Bjorn Brox | last post by:
Hi! In germany, norway and France(?) we are using ',' as decimal separator and it always messes up when you convert a double to and from a string where the interface expects double values stored...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.