Click here to Skip to main content
15,888,259 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
picture
Posted
Comments
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.

To convert from decimal to hex, simply try string hexValue = value.ToString("X");.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Apr-13 16:36pm    
Correct, a 5.
—SA
Andreas Gieriet 14-Apr-13 18:02pm    
I doubt this is the corrent solution. See my comment and my solution below.
Cheers
Andi
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.
Cheers
Andi
Use C# BitConverter[^] class.
E.g.
C#
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 ...?

Cheers
Andi
 
Share this answer
 
Comments
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 ;-)
Cheers
Andi
PS: have my 5 too ;-)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900