Click here to Skip to main content
13,097,926 members (57,694 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello every body!
I have a hex file save multy objects (call is X).X has fieds:name,month,year and 4 values.
when some one write file, you can see image below. X save in total 3 row,
+First row save name, month,day,year...
+Second row save 4 values (data type is real)
+three row is 0.
I can know first row and three row, but i can't read 4 values of second row.
the bold row is 103.15,103.12,94.66 and 30.06
but when i convert it from hex to decimal then result is incorrect.
I must decrypt to get this values. but i can't know writer how to created it?
what mean of this 4 values?
you can see my picture:
Posted 14-Apr-13 7:19am
Richard MacCutchan 14-Apr-13 13:58pm
Why are you converting it to decimal? The information you have provided looks like a hex dump of a file that contains various different items of information, so converting it to decimal is meaningless. You need to know the exact format of each element of each record and process them in whatever way is necessary to restore them to their original content.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To convert from decimal to hex, simply try string hexValue = value.ToString("X");.
Sergey Alexandrovich Kryukov 14-Apr-13 16:36pm
Correct, a 5.
Andreas Gieriet 14-Apr-13 18:02pm
I doubt this is the corrent solution. See my comment and my solution below.
Abhinav S 14-Apr-13 22:44pm
Thank you SA.
Andreas Gieriet 14-Apr-13 18:01pm
Sorry for my ignorence, but I think this is not addressing OP's question.
Doesn't he ask for reading binary data (he names them "hex") to to the equivalent in memory (he calls them "decimal").
I read his question that he got that binary file ("[...]someone write file[...]") and need to slurp it into memory and make respective floating values from the mentioned four chunks.
Maybe I'm misinterpreting OP's question.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use C# BitConverter[^] class.
static void Main(string[] args)
        byte[] b = { 0x00, 0x00, 0x00, 0xE0, 0x97, 0xA5, 0x61, 0x40, };
        double d = BitConverter.ToDouble(b, 0);
        Console.WriteLine("from file: {0,-20} = {1}", d, b.Aggregate("", (s, x) => s += " 0x" + x.ToString("X02")));
        double d = 103.15;
        byte[] b = BitConverter.GetBytes(d);
        Console.WriteLine("double:    {0,-20} = {1}", d, b.Aggregate("", (s, x) => s += " 0x" + x.ToString("X02")));
        float d = 103.15f;
        byte[] b = BitConverter.GetBytes(d);
        Console.WriteLine("float:     {0,-20} = {1}", d, b.Aggregate("", (s, x) => s += " 0x" + x.ToString("X02")));

The result of the above is:
from file: 141.174789428711     =  0x00 0x00 0x00 0xE0 0x97 0xA5 0x61 0x40
double:    103.15               =  0x9A 0x99 0x99 0x99 0x99 0xC9 0x59 0x40
float:     103.15               =  0xCD 0x4C 0xCE 0x42

Please note: none of the patterns match your description:
- The 1st entry converts the first bytes into a double: 141.17... (not 103.15 as you claim this would be).
- The other two entries convert 103.15 as double and float respectively into bytes... (not as expected).

It seems that either your claim for 103.15 is wrong, or the stored data is not a standard conformat floating point number or ...?

Abhinav S 14-Apr-13 22:45pm
5. Hopefully one of our solutions will be what the OP is looking for.
Andreas Gieriet 15-Apr-13 11:47am
Thanks for your 5!
Yes, the question is a bit cryptic an we both read quite the oposite out of it ;-)
PS: have my 5 too ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 14 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100