reading binary file but Hex corresponds to a float
11 views (last 30 days)
Show older comments
Im very new to this and struggling so please bear with me. Ive tried searching but at this stage even that is difficult! I have a file which is binary and Im trying to read using fread. The first part was relatively straightforward as its just int8 but my problem is that one component of the datagram is made up of 4 bytes which have the hex equivelent of a float. For instance at a certain point in the file I get 3d<50><e5><60> or 0x3d50e560 which is a float and the equivalent of 0.051, ie the number I need. Do I need to store each one individually as hex or can I read all at once? Using float keeps me in sync in the file but I lose the actual value?? Thanks Craig.
0 Comments
Answers (4)
Guillaume
on 26 Jul 2016
fread(fid, 1, 'float')
will directly read that float (single) value.
2 Comments
Guillaume
on 26 Jul 2016
Craig's comment moved from answer to here:
Thats what I thought. using, dg.datatemp = fread(fileID,1,'float');
returns a value of 2.8268e+17 when it should be around 28. Mmm, Im missing something
Guillaume
on 26 Jul 2016
Probably a problem with endianness. The number may be encoded as big endian and you're reading it on a little endian machine.
You can specify the correct endianness in the fopen call:
fid = fopen('somefile', 'r', 'b'); %open as big endian
and that will apply to all fread (no effect when reading bytes), or you can specify it for individual fread:
fread(fid, 1, 'float', 0, 'b')
James Tursa
on 26 Jul 2016
Edited: James Tursa
on 26 Jul 2016
E.g., to convert hex characters to a single float that matches your expectations:
>> typecast(int32(hex2dec('42117B5C')),'single')
ans =
36.3705
Or, starting from your byte values:
>> u = uint8([66, 17, 123 ,92])
u =
66 17 123 92
>> swapbytes(typecast(u,'single')) % <-- Endian change
ans =
36.3705
2 Comments
Shreyas Dethe
on 14 Jun 2018
i tried the same foe a 32bit hexadecimal number. But it shows NaN as the output. The equivalent hex number is '1f91813ed6b1533e1bd2723efd41653e' and its output should actually be a floating point number between 0 and 1, however it shows 4.1962e+37, please help.
Guillaume
on 14 Jun 2018
Edited: Guillaume
on 14 Jun 2018
@SHreays Dethe,
1f91813ed6b1533e1bd2723efd41653e is not a 32-bit hexadecimal number. This is a 32 digit hexadecimal number which would have 256 bits.
While there is a standard for encoding floating point numbers on 256 bits, you will first have to establish that your number is indeed encoded using that format. You will also need to establish what endianness is used. There is no built-in function to decode such numbers in matlab so you will have to write your own. Of course, you can't store numbers with that much precision anyway using native types (double is only 64 bits) so if you want to keep your precision, you'll have to use the symbolic library (or other high precision library).
That's assuming your number is indeed 32 hexadecimal digits.
If you need more help however, start your own question.
Craig
on 26 Jul 2016
1 Comment
Guillaume
on 26 Jul 2016
Please use comments on answers rather than starting new answers It makes it hard to follow the conversation otherwise.
You do not need to read it as bytes. As I've said in my comment to my answer, you need to watch out for endianness.
You could of course read it as byte and then swapbytes and typecast afterward but that is a complete waste of time when fread can do it all at once.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!