swapbytes order problem with uint64?
Show older comments
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
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?
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Sep 2017
Do not use hex2dec() for this purpose. Use sscanf()
sscanf('b0120c0a7799ba3e', '%lx')
ans =
uint64
12687216339351878206
%lx is an unsigned 64 bit format.
2 Comments
Tony Tse
on 10 Sep 2017
Walter Roberson
on 10 Sep 2017
If you are working with something that is an IEEE double, then
swapbytes(hex2num('b0120c0a7799ba3e'))
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!