MATLAB: How to read hexadecimal/bcd format as double floating values

bcdfloatingformathexadecimalMATLABpoint

I have some data stored in BCD format that I want to read directly as a floating-point value. How can I achieve this?

Best Answer

To achieve this, please follow the steps below:
1) Create a file with 'FF' written in BCD format:
str = ['FF'];
fileID = fopen('bcd.bin','w');
fwrite(fileID,hex2dec(str),'ubit16');
fclose(fileID);
2) First open the file and then read the data from that file:
fileID = fopen('bcd.bin');
onebyte = fread(fileID,1,'*ubit16')
3) You will see that "onebyte" is a variable of type "uint16" with a value of 255. To turn this into a MATLAB double, we can use the "double" function:
sol = double(onebyte) % this is 255.