MATLAB: Converting Hex value to negative fractional decimal

2's complementhex conversionhex to dec

Hello,
I am trying to convert the Hex value ('BF6E23F8').
I know that the answer should be (-0.93023634).
Please correct me if I am wrong, but I need to convert the Hex value to 2's complement binary, then to decimal?
I've attempted to use quite a few functions I've found online including hex2dec, Fr_bin2dec.
But I am hoping you can direct me towards the solution.
Thanks,
Ryan D'Souza

Best Answer

It looks like your hexadecimal values are the representation of a single precision IEEE 754 number, in little endian format.
hexvalues = ['BF6E23F8';'3CCCE6BD';'BF6E158D';'C1C63C18']
hexbits = uint32(hex2dec(hexvalues)); %convert hexadecimal to 32-bit integer representation
[~, ~, endianness] = computer;
if endianness == 'B'
hexbits = swapbytes(hexbits); %on a big-endian computer, switch endianness
end
hexassingle = typecast(hexbits, 'single') %cast from integer to single