MATLAB: 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

fscanfhexhex2dechexadecimalMATLABsscanf

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'.

Best Answer

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.