Current location - Education and Training Encyclopedia - Graduation thesis - How to convert a floating-point value into a 4-byte 16 hexadecimal number?
How to convert a floating-point value into a 4-byte 16 hexadecimal number?
I wrote a paper on this.

In the process of upgrading the communication protocol of the company's existing products, it involves the transmission of floating-point numbers. The traditional method is to multiply a floating-point number by an integer multiple, enlarge it to a certain precision, round it, transmit the rounded integer to the destination, and then divide it by the corresponding multiple to recover the true value. Although this method can avoid the operation of floating-point numbers in the transmission process, it brings computational pressure to both parties. Multiplication and division are needed to enlarge and shrink a floating-point number several times. Moreover, the standard Modbus RTU protocol also allows floating-point operations. Therefore, when using floating-point numbers in communication, the most important thing is to pay attention to the storage format of floating-point numbers in memory, which is the premise for both parties to correctly analyze communication data. The structure of floating-point numbers has been clearly defined by the IEEE Committee, and now most hardware manufacturers and compilers have adopted the IEEE regulations on floating-point numbers. IEEE stipulates that binary floating-point numbers can be represented by 32 bits, 64 bits and 128 bits. For the research and development products used by our company and the prediction of future product development, the use of 32-bit floating-point numbers is enough to meet the requirements of data size and accuracy, so the floating-point numbers in our communication protocol are represented by 32-bit binary, and according to the provisions of IEEE, 32-bit floating-point numbers are expressed as

(- 1)s X be X m

In ...

S is 0 or 1.

B is the radix used.

E is an integer and Emin

M is expressed as the following set of values, d0.d1d2d3d4 ... dp-1(di is an integer, 0.

For floating-point numbers with 32-bit binary representation, the value of b is 2, Emin = 0 and emax = 127. For example, the binary representation of-1 1 is-1kloc-0/,which is converted into the above IEEE representation, as shown below.

(-1)1x (10) 3x (1.01), and the IEEE notation is encoded as a 32-bit binary number.

Where e is represented by a frame shift,

Please refer to the IEEE floating-point operation standard for the relevant contents of IEEE regulations on floating-point numbers.