javascript浮动/到位 [英] javascript float from/to bits

查看:110
本文介绍了javascript浮动/到位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用任何其他语言执行一些简单易懂的东西但不是javascript:从浮动中获取这些位(反之亦然)。

I am trying to perform something that is brain-dead simple in any other language but not javascript: get the bits out of float (and the other way around).

在C / C ++中,它将类似于

In C/C++ it would be something like


float a = 3.1415;
int b = *((int*)&a);

,反之亦然


int a = 1000;
float b = *((float*)&a);

在C#中你可以使用BitConverter
... floatBits或类似Java的东西...即使在VB6中也可以用于基督你可以将float32 memcpy到int32中。我怎么能在javascript中转换和int和浮点数?

In C# you can use the BitConverter ...floatBits or something alike in Java... Even in VB6 for Christ's sake you can memcpy a float32 into an int32. How on earth can I translate between and int and a float in javascript?

推荐答案

你当然不会像JavaScript那样得到任何低级别的东西。允许以不受信任的潜在攻击者网站使用的语言进行重铸和指针擦除是非常危险的。

You certainly don't get anything low-level like that in JavaScript. It would be extremely dangerous to allow recasting and pointer-frobbing in a language that has to be safe for untrusted potential-attacker web sites to use.

如果你想获得数字中的单精度值的32位IEEE754表示(记住也不是int;您在JavaScript中获得的唯一数字类型是 double ),您必须通过摆弄标志,指数和尾数位来自己做。示例代码为此处

If you want to get a 32-bit IEEE754 representation of a single-precision value in a Number (which remember is not an int either; the only number type you get in JavaScript is double), you will have to make it yourself by fiddling the sign, exponent and mantissa bits together. There's example code here.

这篇关于javascript浮动/到位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆