Use javaScript to convert a floating point to a hexadecimal character

Source: Internet
Author: User

A recent embedded project requires WEB functions. Therefore, we want to use HTML + JavaScript to implement some parameter configuration functions. The parameters generate hexadecimal data from JavaScript and submit the data to the microcontroller through POST, and then directly use it to make full use of the browser's computing capabilities.

Because JavaScript has poor support for floating-point numbers, it directly uses the floating-point-to-hexadecimal function. Double keys cannot implement the 4-byte storage notation that is commonly implemented in the C language. By searching, the related function code is not found. the Buffer class of js can implement this function, but it cannot be used (the storage space of single-chip microcomputer is limited), nor can we find out how to implement it (not quite familiar with it ).

For example:

123.456 = 0x42 f6e979 in C language, you can directly convert a floating point into an unsigned int and then output it into a hexadecimal number, but the implementation in JavaScript is not so straightforward.

We have the honor to find an IEEE754 standard floating point conversion code (IEEE754 floating point converter (C # implementation) on the Internet. Click the link to open the code and convert it to JavaScript, after the job is completed, the code is pasted out and shared.

In addition, I only implement the hexadecimal system, but not the reverse code in the C # code.

The following code can be implemented as follows:
Get_float_hex (123.456) ==> 42F6E979
Copy codeThe Code is as follows:
Function DecToBinTail (dec, pad)
{
Var bin = "";
Var I;
For (I = 0; I <pad; I ++)
{
Dec * = 2;
If (dec> = 1)
{
Dec-= 1;
Bin + = "1 ";
}
Else
{
Bin + = "0 ";
}
}
Return bin;
}
Function DecToBinHead (dec, pad)
{
Var bin = "";
Var I;
For (I = 0; I <pad; I ++)
{
Bin = (parseInt (dec % 2). toString () + bin;
Dec/= 2;
}
Return bin;
}
Function get_float_hex (decString)
{
Var dec = decString;
Var sign;
Var signString;
Var decValue = parseFloat (Math. abs (decString ));
If (decString. toString (). charAt (0) = '-')
{
Sign = 1;
SignString = "1 ";
}
Else
{
Sign = 0;
SignString = "0 ";
}
If (decValue = 0)
{
Fraction = 0;
Exponent = 0;
}
Else
{
Var exponent = 127;
If (decValue> = 2)
{
While (decValue> = 2)
{
Exponent ++;
DecValue/= 2;
}
}
Else if (decValue <1)
{
While (decValue <1)
{
Exponent --;
DecValue * = 2;
If (exponent = 0)
Break;
}
}
If (exponent! = 0) decValue-= 1; else decValue/= 2;

}
Var fractionString = DecToBinTail (decValue, 23 );
Var exponentString = DecToBinHead (exponent, 8 );
Return Right ('2013' + parseInt (signString + exponentString + fractionString, 2). toString (16), 8 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.