在 JS 中读/写 float 字节 [英] Read/Write bytes of float in JS
本文介绍了在 JS 中读/写 float 字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以在 JS 中读取浮点值的字节吗?我需要将原始 FLOAT 或 DOUBLE 值写入我需要制作的某种二进制格式,那么有没有办法获得逐字节的 IEEE 754 表示?当然还有同样的写作问题.
Is there any way I can read bytes of a float value in JS? What I need is to write a raw FLOAT or DOUBLE value into some binary format I need to make, so is there any way to get a byte-by-byte IEEE 754 representation? And same question for writing of course.
推荐答案
会这个片段帮忙?
var parser = new BinaryParser
,forty = parser.encodeFloat(40.0,2,8)
,twenty = parser.encodeFloat(20.0,2,8);
console.log(parser.decodeFloat(forty,2,8).toFixed(1)); //=> 40.0
console.log(parser.decodeFloat(twenty,2,8).toFixed(1)); //=> 20.0
这篇关于在 JS 中读/写 float 字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文