Home >Web Front-end >JS Tutorial >JavaScript converts floating point numbers to hexadecimal characters_javascript skills
A recent embedded project required WEB functions, so I thought of using HTML JavaScript to implement some parameter configuration functions. The parameters are generated in hexadecimal data by JavaScript, submitted to the microcontroller through POST, and then used directly to fully utilize the computing power of the browser. .
Due to JavaScript’s weak support for floating point numbers, directly using the floating point number to hexadecimal conversion function cannot achieve the 4-byte storage representation usually implemented in C language. After searching, there is no I found the relevant function code. This function can be implemented in the Buffer class of Node.js, but there is no way to use it (the storage space of the microcontroller is limited), and there is no way to find out how to implement it (I can't understand it).
For example:
123.456 ==》 0x42F6E979 In C language, just convert a floating point number to unsigned int and then output it as a hexadecimal number. But implementing it in JavaScript is less straightforward.
Fortunately, I found an IEEE754 standard floating-point number conversion code implemented in C# on the Internet (IEEE754 floating-point number converter (C# implementation), click to open the link), and completed this task by converting this code into JavaScript. Work, for this, post the code and share it.
In addition, I only implemented the conversion to hexadecimal system and did not implement the reversal (reverse code) which is also available in the C# code.
The following code can be used to achieve the following:
get_float_hex(123.456) ==> 42F6E979