How to convert a file containing real numbers into binary/hexadecimal?

I want to convert whole file of real numbers having atleast 1000 data points into binary/hex using some function or program. Plz help

 Accepted Answer

num2hex() is the routine to use to see the hex representation of the IEEE 754 bit pattern for float or double.

1 Comment

yes num2hex() is working for files containing real numbers. Thanks a lot for your reply!!

Sign in to comment.

More Answers (2)

You can use dec2bin and dec2hex function
but there is some problem in the converted data. I have converted a file containing 1000 real numbers to hexadecimal using this function num2hex(). The real numbers are like:
-0.0128380000000000
-0.0142530000000000
-0.0080360000000000
-0.0120290000000000
-0.0136970000000000
-0.0268890000000000
-0.0320450000000000
-0.0295180000000000 & so on upto 1000.
--
The converted hex file is looking like:
bf8a4acf312b1b37
bf8d30ad46f587d7
bf80752da98676a7
bf88a2a90cd423d9
bf8c0d2c386d2ed8
bf9b88ca3e7d1351
bfa06833c60029f1
bf9e39f77292c493
--
but I think it should be like:
0xbc52567a
0xbc69856a
0xbc03a96d
0xbc451548
0xbc606962
0xbcdc4652
0xbd03419e
--
Is there any solution to this? Plz reply

3 Comments

The outputs are correct for double precision numbers, but you happen to be looking for output for single precision numbers. Apply single() to the numbers and num2hex() the result. Alternately you can use something like
char(arrayfun(@(x) sprintf('0x%08x', typecast(single(x), 'uint32')), YourArray(:), 'Uniform', 0))
or
num2str(typecast(single(YourArray(:)),'uint32'), '0x%08x')
Thank you Sir!! I have one more problem that after processing this converted data file, I need to convert this hex file back to real data file. So what function should I use? Plz help!
https://www.mathworks.com/matlabcentral/newsreader/view_thread/30803

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!