Reading Binary(?) to hex to float.
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2012-5-24
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);
回答(1 个)
Geoff
2012-5-22
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 个评论
Walter Roberson
2012-5-24
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 ?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Create Fixed-Point Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!