admin管理员组文章数量:1363053
I need to encode and decode IEEE 754 floats and doubles from binary in node.js to parse a network protocol.
Are there any existing libraries that do this, or do I have to read the spec and implement it myself? Or should I write a C module to do it?
I need to encode and decode IEEE 754 floats and doubles from binary in node.js to parse a network protocol.
Are there any existing libraries that do this, or do I have to read the spec and implement it myself? Or should I write a C module to do it?
Share Improve this question asked Sep 18, 2010 at 8:10 nornagonnornagon 15.8k18 gold badges76 silver badges86 bronze badges 2- 1 Maybe you can see if this thing does what you want: jsfromhell./classes/binary-parser – Tor Valamo Commented Sep 18, 2010 at 20:39
- See also: javascript - Read/Write bytes of float in JS - Stack Overflow (all the answers there would work here too) – user202729 Commented Jan 30, 2021 at 10:55
3 Answers
Reset to default 3Note that as of node 0.6 this functionality is included in the core library, so that is the new best way to do it.
See http://nodejs/docs/latest/api/buffer.html for details.
If you are reading/writing binary data structures you might consider using a friendly wrapper around this functionality to make things easier to read and maintain. Plug follows: https://github./dobesv/node-binstruct
In modern JavaScript (ECMAScript 2015) you can use ArrayBuffer
and Float32Array
/Float64Array
. I solved it like this:
// 0x40a00000 is "5" in float/IEEE-754 32bit.
// You can check this here: https://www.h-schmidt/FloatConverter/IEEE754.html
// MSB (Most significant byte) is at highest index
const bytes = [0x00, 0x00, 0xa0, 0x40];
// The buffer is like a raw view into memory.
const buffer = new ArrayBuffer(bytes.length);
// The Uint8Array uses the buffer as its memory.
// This way we can store data byte by byte
const byteArray = new Uint8Array(buffer);
for (let i = 0; i < bytes.length; i++) {
byteArray[i] = bytes[i];
}
// float array uses the same buffer as memory location
const floatArray = new Float32Array(buffer);
// floatValue is a "number", because a number in javascript is a
// double (IEEE-754 @ 64bit) => it can hold f32 values
const floatValue = floatArray[0];
// prints out "5"
console.log(`${JSON.stringify(bytes)} as f32 is ${floatValue}`);
// double / f64
// const doubleArray = new Float64Array(buffer);
// const doubleValue = doubleArray[0];
PS: This works in NodeJS but also in Chrome, Firefox, and Edge.
I ported a C++ (made with GNU GMP) converter with float128 support to Emscripten so that it would run in the browser: https://github./ysangkok/ieee-754
Emscripten produces JavaScript that will run on Node.js too. You will get the float representation as a string of bits, though, I don't know if that's what you want.
本文标签: floating pointEncoding and decoding IEEE 754 floats in JavaScriptStack Overflow
版权声明:本文标题:floating point - Encoding and decoding IEEE 754 floats in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743808920a2542698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论