在 JavaScript 中将十六进制转换为浮点数
我想将带有分数的 10 基数转换为 16 基数。
var myno = 28.5;
var convno = myno.toString(16);
alert(convno);
一切都很好。现在我想将其转换回十进制。
但现在我不能写:
var orgno = parseInt(convno, 16);
alert(orgno);
因为它不返回小数部分。
而且我不能使用 parseFloat,因为根据 MDC,parseFloat 的语法是
parseFloat(str);
如果我必须转换回 int,则不会有问题,因为 parseInt 的语法是
parseInt(str [, radix]);
那么有什么替代方案呢?
免责声明:我认为这是一个微不足道的问题,但谷歌搜索没有给我任何答案。
这个问题让我提出了上述问题。< /子>
I would like to convert a number in base 10 with fraction to a number in base 16.
var myno = 28.5;
var convno = myno.toString(16);
alert(convno);
All is well there. Now I want to convert it back to decimal.
But now I cannot write:
var orgno = parseInt(convno, 16);
alert(orgno);
As it doesn't return the decimal part.
And I cannot use parseFloat, since per MDC, the syntax of parseFloat is
parseFloat(str);
It wouldn't have been a problem if I had to convert back to int, since parseInt's syntax is
parseInt(str [, radix]);
So what is an alternative for this?
Disclaimer: I thought it was a trivial question, but googling didn't give me any answers.
This question made me ask the above question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
另一种可能性是单独解析数字,将字符串分成两部分,并在转换过程中将两部分都视为整数,然后将它们加回一起。
Another possibility is to parse the digits separately, splitting the string up in two and treating both parts as ints during the conversion and then add them back together.
试试这个。
该字符串可以是具有四个字符 (0 - 255) 的原始数据(简单文本)或
长度为四个字节的十六进制字符串“0xFFFFFFFF”。
jsfiddle.net
Try this.
The string may be raw data (simple text) with four characters (0 - 255) or
a hex string "0xFFFFFFFF" four bytes in length.
jsfiddle.net
请尝试这个:
Please try this:
我结合了 Mark 的 和 Kent 的答案 制作一个重载的 parseFloat 函数,该函数接受基数参数(更简单、更多才多艺的):
I combined Mark's and Kent's answers to make an overloaded parseFloat function that takes an argument for the radix (much simpler and more versatile):
试试这个:
反向执行步骤即可转换回来。
一个简单的例子:
将十进制23.5转换为十六进制,转换后想要小数点后一位。
23.5 x 16 = 376。
转换为十六进制 = 0x178。
底数为 16 的答案: 17.8
现在转换回小数:
去掉小数点: 0x178
转换为小数: 376
除以 16: 23.5
Try this:
Reverse the steps to convert back.
A simple example:
Convert decimal 23.5 into hex, and want one digit after the decimal point after conversion.
23.5 x 16 = 376.
Converted to hex = 0x178.
Answer in base 16: 17.8
Now convert back to decimal:
Take out the decimal point: 0x178
Convert to decimal: 376
Divide by 16: 23.5
我不确定你想在那里解析什么十六进制格式。这是类似:“a1.2c”吗?
浮点数通常使用 IEEE 754 标准以十六进制格式存储。该标准不使用任何点(纯十六进制字母中不存在点)。相反,存在三组预定义长度的位(1 + 8 + 23 = 总共 32 位 ─ double 使用 64 位)。
我编写了以下函数来将此类数字解析为浮点数:
I'm not sure what hexadecimal format you wanted to parse there. Was this something like: "a1.2c"?
Floats are commonly stored in hexadecimal format using the IEEE 754 standard. That standard doesn't use any dots (which don't exist in pure hexadecimal alphabet). Instead of that there are three groups of bits of predefined length (1 + 8 + 23 = 32 bits in total ─ double uses 64 bits).
I've written the following function for parsing such a numbers into float:
以下是 Mark Eirich 的答案的大小改进:
Here is a size-improvement of Mark Eirich's answer:
有人可能会发现这很有用。
字节到 Float32
Someone might find this useful.
bytes to Float32