Why do I receive an incorrect result when using the FSCANF or SSCANF function within MATLAB 7.0.4 (R14SP2) to import a 16 character hex number to decimal?

2 views (last 30 days)
When I execute the following commands
val = '00EDA4DA2079952B' % 16-character (64-bits) element value
a=sscanf(val,'%x')
I receive the following result
4.2950e+009
which is not the equivalent of the hexadecimal value '00EDA4DA2079952B'.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
Internally, FSCANF and SSCANF use the ASCII versions of their respective functions. The return values of the ASCII versions are of type C long. Since these functions in C set the value to a long, the largest value that can be represented is 4.2950e+009. This limits the way in which MATLAB reads data from a file using FSCANF.
To work around this issue, use FSCANF or SSCANF to read the hex number as characters, then use the function HEX2DEC to convert it to decimal. The following example shows how this can be done:
fid = fopen('data.txt', 'r');
a = fscanf(fid, '%c')
hex2dec(a)
fclose(fid);
where "data.txt" contains the hex number.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!