JavaScript implements floating-point hexadecimal character _javascript techniques

Source: Internet
Author: User
A recent embedded project requires Web functionality, so think of using html+javascript to implement some parameter configuration functions, the parameters from JavaScript generated hexadecimal data, submitted to the microcontroller by post, and then directly used to fully utilize the browser's computing power.

Because JavaScript support for floating-point numbers is very weak, the direct use of floating-point hexadecimal function, dual can not achieve the usual in C language implementation of the 4-byte storage notation, through the search, and did not find the relevant functional code, In the node.js of the buffer class can achieve this function, but there is no way to use (SCM storage space is limited), there is no way to find out how the specific implementation of (see do not understand).

For example:

123.456 = "0x42f6e979 in C, it is possible to convert a floating-point number directly into a unsigned int and then output it as 16, but it is less straightforward to implement in JavaScript.

I was fortunate enough to find a IEEE754 standard floating-point conversion code (IEEE754 floating Point Converter (C #) to open the link in C #, which was done by translating this code into JavaScript, and then posting the code and sharing it.

In addition, I have only implemented the 16 system, no reversal (reverse code) in C # code.

The following code can be implemented as follows:
Get_float_hex (123.456) ==> 42f6e979
Copy Code code 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 (' 00000000 ' +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.