How to read hexadecimal/bcd format as double floating values?

4 Ansichten (letzte 30 Tage)
I have some data stored in BCD format that I want to read directly as a floating-point value. How can I achieve this?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 10 Nov. 2020
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.

Weitere Antworten (0)

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by