Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Delphi
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 14-Apr-13 8:19am
Comments
Richard MacCutchan at 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
good
Please Sign up or sign in to vote.

Solution 1

To convert from decimal to hex, simply try string hexValue = value.ToString("X");.
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 14-Apr-13 16:36pm
   
Correct, a 5.
—SA
Andreas Gieriet at 14-Apr-13 18:02pm
   
I doubt this is the corrent solution. See my comment and my solution below.
Cheers
Andi
Abhinav S at 14-Apr-13 22:44pm
   
Thank you SA.
Andreas Gieriet at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use C# BitConverter[^] class.
E.g.
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
  Permalink  
Comments
Abhinav S at 14-Apr-13 22:45pm
   
5. Hopefully one of our solutions will be what the OP is looking for.
Andreas Gieriet at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Apr 2013
Copyright © CodeProject, 1999-2014
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