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.
0 个评论
回答(4 个)
Guillaume
2016-7-26
fread(fid, 1, 'float')
will directly read that float (single) value.
2 个评论
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
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')
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
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
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
2016-7-26
1 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!