Instructions for use:
Computer science students and enthusiasts who like software reverse engineering all know that floating point numbers are stored in hexadecimal format inside the computer. It adopts the IEEE binary floating-point arithmetic standard (IEEE 754), which is used by many CPUs and floating-point operators. However, the calculator currently provided with the system does not have this function, so I wrote this small tool. In addition, the function of converting decimal to hexadecimal is included.
How to represent floating point numbers in hexadecimal
In binary files, the format of stored data is hexadecimal.
The following example illustrates how 27.0f is represented in a binary file.
float
A total of 32 bits, equivalent to 4 bytes
From the highest to the lowest bits are the 31st, 30th, 29th,...,0th bits respectively
Bit 31 is the sign bit, 1 means the number is negative, 0 means otherwise.
Bits 30-23, a total of 8 bits are exponent bits.
Bits 22-0, a total of 23 bits are the mantissa bits.
Every 8 people are divided into 4 groups, namely Group A, Group B, Group C and Group D.
Each group is a byte, stored in reverse order in memory, that is: DCBA
27.0 expressed in binary is: 11011.0
Expressed in scientific notation as 1.110110*2^4, now the mantissa and exponent we want are available.
The mantissa is: 1101 10 (delete the first 1 in front, because using scientific notation, the first one in binary is always 1, the computer does not store this 1 when storing, only the number of digits after the decimal point) If there are less than 23 digits, add 0, which is 1011 0000 0000 0000 0000 000 (23 digits)
Index: is 4. A total of 8 bits, which can represent unsigned integers ranging from 0 to 255, or signed integers ranging from -128 to 127. But because the exponent can be negative, in order to unify the decimal integer into binary, 127 is added first.
So: 4+127=131 becomes 10000011 in binary
The binary representation of 27.0 is:
Add sign bit 0 at bit 31
That's 0100 0001 1101 1000 0000 0000 0000 0000
Hexadecimal is: 41 D8 00 00
Look at another number 27.5, which is 11011.1 in binary
1.10111*2^4
The mantissa (the number after the decimal point) is 10111, with 23 digits added: 1011 1000 0000 0000 0000 000
Exponent: 4, plus 127, it is 131, binary 1000 0011
The binary representation is (1 sign digit) 0 (8 exponent digits) 1000 0011 (23 mantissa digits) 1011 1000 0000 0000 0000 000
Written in binary standard form: 0100 0001 1101 1100 0000 0000 0000 0000
Written in hexadecimal, it is 41 DC 00 00
it works
it works
it works