Conversion of double-precision floating-point and single-precision floating-point hexadecimal numbers

Asked 5 months ago, Updated 5 months ago, 14 views

This is PYTHON3. Let's say the hexadecimal number 00000080be2c9d3f.I'd like to convert this to a double-precision floating point.
Are there any functions or modules that can be done easily?Please let me know if there is a single-precision floating point.If you convert it to binary by yourself, the value will go in the direction of the day after tomorrow.I don't think the previous zero has been converted to binary... Could you please let me know?

python3

2022-09-30 11:03

1 Answers

First of all, the hexadecimal number "00000080be2c9d3f" will change your answer depending on where you got this data.

The reason is that Intel's CPU, which is commonly used on servers and desktop PCs, has a little endian byte order.Therefore, on memory and CPU, each byte is arranged from the lower side.On the other hand, when humans handle hexadecimal, it's easier to understand if you arrange them from the upper side of each byte.That's the Big Endian.For example, if a human being represents 000000FF in hexadecimal format, it is listed as FF000000 on Intel machines.

Therefore, for Big Endians, the hexadecimal number "00000080be2c9d3f" has an exponential part of 0, so the number of non-normalizations is very close to 0.On the other hand, in the case of Little Endians, 3f9d2cbe80000000, the sign is positive and the exponent is a floating-point number of 0111111001.

Pythpn uses the structure module to convert to binary.

For Big Endians,

structure.unpack('>d', bytes.fromhex('00000080be2c9d3f')))[0]
2.73191824563e-312

In the case of Little Endian, native is fine, so

structure.unpack('d', bytes.fromhex('00000080be2c9d3f')[0]
0.028490997850894928

For single-precision floating-point, simply change d to f.

Official documentation structure


2022-09-30 11:03

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.