Reading Binary(?) to hex to float.

Hey,
I'm back with a more defined question, I kept getting asked to provide input values. So basic idea is reduce computing time by re-making the code below to convert the data faster. Is this possible?
(32 bit IEEE demultiplexed)
x = hex2float( dec2hex( fread(fid, 1, 'ubit32', 0, 'ieee-be') ))
------------------------------------------------------------------------------------------------
The binary format (input file), when opened, looks like this:
€X "9 ` ÿ + ô ú 6 % 2 4éýp – $ÿ8 + Ð @I ÀrÀ DØó3 @ : :2ô 8i€ ¹¬@ 8Z€ ºGà º„Ä ¹Äx ¹ôØ 8e ¹¿è ºC$ 9ÇX 9Šx : h :¼ ·þ :)° ¹Œ ºhŒ 9šP ºLÔ »Š ¹ÅÀ :Ä ¹Jð ºQ º­ ºL( ºc º© ¹á :)h ;¬ :v< :y ;"¿ :X 9Ë` :_ º :/ :0 º Ú 9Là :b` ºm º ºt ¹íˆ ¹’Ø ºòv º„Ì :FD : 9®è :¥Š :¾ :b¨ ;· :ê :$h ;bÞ :VD ºÜ º­ž » ƒ º\ »g » ºD » º^„ 9§ ¸º  9·À º : Ü : ¶‚ 9±  :_ ;• ;p :AX : ; :¾8 9/à º{ ». ºêh ºù ºÉŽ ¸”@ ¹w º` 9ö¨ ¸Ð  º‡Ô ¹ € ¹ à ¹7` 8˜€ ¸–À :¸ :œp :Ä„ :á( :pX :¨¼ ;!¬ :¸® ¹³˜ ¸€ 9f@ º”D ºJØ ¹Ï8 ºü ºjˆ ºeè ¹` 9׈ 9þ 9Ì ¹¤8 ¹’P 9íÈ :¡Ø :¢˜ 96@ 9Þ( :È ¹EP ¸Ë ºÛn ºÈ 8Ê€ º×L º/À :8ü ºŒf ºf„ ¹5Ð 9¯` 9ÛÈ 9:ð :Uè ¸´` ºC< ¹ƒ º‚f ºQ\ :<$ ·… ¹Ñ0 :%Ü º À ¹½h 9Á8 º¸´ º´ º° º¬Ä ¹Ê º‡¢ º=à :•® ;” :ߦ :¢& ;Ð ; :8 :^p 7Ó€ »v » ºî »G¿ »¼ º 9<p :¤ :Œ¾ :ÎF ;5 ;,= :Àh :…Ø :É :X 9´p 9H º²ì » ºÉ ºeØ ºÎx ºÁ¸ ¹@P ; ;1 :Z :Ë2 :Z  9g  9 º º±B ºMÌ ¸Î€ ¹«¸ ¶, :—– :_L :8\ 9ùh 9#à ·ô€ ºŠ° ºÌ* º¸R º¹ º¶Î º> ¹–ˆ :È :ÌÎ :i :›: : ’ :€ :€ò 9è° 9gÐ ¸ÐÀ ºW¸ ºK` º´â ¹ÏP :lü ;u ; :H :Ñä :©¼ ·S 8p º ¹Ù( º–à ºùv º¤‚ ºv𠹈 ºµæ º î ºH º( ··€ º4 ¸€` 7ê€ º>ˆ º;„ º ¹ã€ ºbD 9 ° :, 9[° :¯ :Ñœ :€ :Ȭ :¼ :…ú :˜ ¹ð º0 ºCP º³ ºªÚ º‹ ¹Øˆ ºvl º!x 9‰  ¹« :,` 8‰à º3¼ 9Ú` 8õ :Ðê ;5 :h : 2 ;_ :•¾ 9>  9™  8É 9= º€: »â ºœ. ºÎ¶ ¹ÕX :q˜ :K< : ‚ 9¥ :vä :̘ ºMè º5Ü 9 º’ ºÒ ºäî º¬2 ºXˆ ºšö º0ˆ ¹! ¹—` 8¨À :š¶ :ˆ8 : T :®\ 9Ì ºDT 9(À 9±à ¹@ 9J° 9𠺕ž º1à ¸®@ ºÍB º·l º,Ð 91€ :Õj :ì :ƒ~ :Ŧ :³J :„ :bP 8$À º@˜ ¹Up 9O º¥* º¡° ¹“ º´ º›¸ ºAÜ 9³° 9DP 73 7² ¹­Ø 9b@ :±Ê :U° ¹! :Áx ;z 9³¨ 6` 9°à ¹›P »

1 commentaire

Walter Roberson
Walter Roberson le 24 Mai 2012
Could I ask you to post the output of the following:
fid = fopen(YourInputFile, 'r');
sprintf('%02X ', fread(fid, 48, '*uint8')); sprintf('\n\n');
fseek(fid,0,'bof');
for K = 1 : 12; disp(dec2hex( fread(fid, 1, 'ubit32', 0, 'ieee-be') )); end
fclose(fid);

Connectez-vous pour commenter.

Réponses (1)

Geoff
Geoff le 22 Mai 2012

1 vote

Um, you're reading one at a time?
Can you not first determine the file size:
fseek(fid, 0, 'eof');
fsize = ftell(fid);
fseek(fid, 0, 'bof');
And then read that many characters while skipping the explicit conversions you're doing:
x = fread( fid, fix(fsize/4), 'float', 0, 'ieee-be' );

8 commentaires

Walter Roberson
Walter Roberson le 22 Mai 2012
The claim in previous threads was that the bytes were in a non-standard order, or something like that. Or that the bytes were an encoding of hex. I don't remember now.
Since you are reading to end of file, you can use inf as your size.
You should specify the data type that is being input and output.
The skip and size are optional.
x = fread( fid, '*float', 'ieee-be' );
Geoff
Geoff le 22 Mai 2012
Cool, thanks for the simplification.
I tested the hex2float(dec2hex(etc)) combination against reading floats (actually, it was hex2num), as well as checking 'ubit32' versus 'uint32' and there was no difference.
Dejan Cvijanovic
Dejan Cvijanovic le 24 Mai 2012
Hey guys,
I tried what you guys suggested and got this output:
0.0000
0.0000
0.0000
0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
0.0000
So the good new is that they're floating point and it processes fast, but they're definitely not correct. Thoughts?
For the record, this is the code I used:
"fid = fopen('aa.sgd');
x = fread( fid, '*float', 'ieee-be' );"
Dejan Cvijanovic
Dejan Cvijanovic le 24 Mai 2012
UPDATE:
I used this code:
"fid = fopen('aa.sgd');
x = fread(fid, inf, '*float', 0, 'ieee-be' );
"
and get numbers like:
>> x(100:120)
ans =
0.0004
0.0009
-0.0005
0.0007
0.0007
-0.0012
0.0002
0.0009
-0.0009
-0.0006
-0.0005
-0.0005
-0.0003
-0.0018
-0.0010
0.0008
0.0005
0.0003
0.0013
0.0015
0.0009"
Does that seem weird?
Walter Roberson
Walter Roberson le 24 Mai 2012
It doesn't seem weird to me, since I have no idea what value range the floats are intended to be.
Might I suggest you command
format long g
before examining your values?
Geoff
Geoff le 24 Mai 2012
It would help if you sent us the four-byte hex value for one of your floats, and told us what that float value is supposed to be, or referred us to somewhere that exactly describes the format.
Walter Roberson
Walter Roberson le 24 Mai 2012
Yes, we need the hex.
If we examine what was posted above, with €X being the first float, that is hex E2101213 . If any byte other than the first is chosen as first byte of the IEEE float32, then the number would be positive and in the range of 2^(-95), giving a number on the order of +1E-28 . If the first byte (E2) is chosen as the first byte of the IEEE float32, then the number would be negative and in the range of 2^69, giving a number on the order of -1E+20.
Dejan, what is the expected value for the first float in the file ?
Dejan Cvijanovic
Dejan Cvijanovic le 25 Mai 2012
I will find out soon and update! Thanks for being patient.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by