在JS中读取/写入浮点字节 [英] Read/Write bytes of float in JS
问题描述
这段代码有帮助吗? @ Kevin Gadd : 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. Would this snippet help?
这篇关于在JS中读取/写入浮点字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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
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