MATLAB: Swapbytes order problem with uint64

swapbytes

I find that swapbytes doesn't seem to be behaving correctly for my problem. I have a 64 bits hexidecimal string printed in big-endian: 'b0120c0a7799ba3e'
Manually swapping the bytes give me the expected answer:
typecast(uint64(hex2dec('3eba99770a0c12b0')), 'double')
ans =
1.5855e-06
If I follow the example code in swapbytes however, it seems behave incorrectly. Either:
typecast(swapbytes(uint64(hex2dec('b0120c0a7799ba3e'))), 'double')
ans =
3.5031e-305
or
typecast(uint64(swapbytes(hex2dec('b0120c0a7799ba3e'))), 'double')
ans =
0
The function seem to work correctly for a single precision hexadecimal:
typecast(swapbytes(uint32(hex2dec('0cc9d435'))),'single')
ans =
single
1.5854e-06
Am I missing something, I can certainly write some loops to parse the string but is there a better solution?

Best Answer

I suspect the problem is actually in hex2dec(). You are exceeding the flintmax limit described in the hex2dec documentation:
d = hex2dec('hex_value') converts hex_value to its floating-point integer representation. The argument hex_value is a hexadecimal integer stored as text. If the value of hex_value is greater than the hexadecimal equivalent of the value returned by flintmax, then hex2dec might not return an exact conversion.