reading binary file but Hex corresponds to a float

10 次查看(过去 30 天)
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.

回答(4 个)

Guillaume
Guillaume 2016-7-26
The third argument of fread tells it which format is embedded in the bytes, therefore:
fread(fid, 1, 'float')
will directly read that float (single) value.
  2 个评论
Guillaume
Guillaume 2016-7-26
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
Guillaume 2016-7-26
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')

请先登录,再进行评论。


Craig
Craig 2016-7-26
To clarify it should read around 36 degrees celcius. So if I manually run through the file and read the bytes as uint8 then I get 66, 17, 123 and 92 which as hex is 0x42117B5C. Now if I use an online floating point convertor this equates to 36, my value. Make sense? Not sure how to get the in matlab. Surely not by reading and storing individually? Craig.

James Tursa
James Tursa 2016-7-26
编辑:James Tursa 2016-7-26
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 个评论
Shreyas Dethe
Shreyas Dethe 2018-6-14
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
Guillaume 2018-6-14
编辑:Guillaume 2018-6-14
@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
Craig 2016-7-26
Thanks so much for that James. I had hoped I would be able to do the conversion as I read the binary values but guess that this needs to be done so will look to store each seperately then convert using the above, thanks so much!. Craig
  1 个评论
Guillaume
Guillaume 2016-7-26
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by