Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

node.js - NodeJS Reading Buffer Binary To Float

I have a large DAT file that holds custom byte information,
And I've been tasked with converting the solution to JavaScript.
It's to make the solution be more single-language and convert to serverless cloud computing.

But, I've run into an issue with converting this test data.
The values supposed to return a float,
But I can't seem to get the number converted correctly.

The sliced buffer output is <Buffer 40 82 e2 31 d6 d7 2e 8d>,
Which is supposed to return 604.274335557087
But actually returns 4.090111255645752.

And I'm at my wits end right now.
Any thoughts?


  fs.readFile(file, (err, data) => {

...
      // Read other buffer slice() values fine until this step. 
      // Like: readInt8(), readIntBE(0, 4), readBigUInt64BE(0, 8) 
...

      let FloatNumber = data.slice(start, end).readFloatBE();
      console.log('FloatNumber', FloatNumber);

...

  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const buf = Buffer.from([0x40, 0x82, 0xe2, 0x31, 0xd6, 0xd7, 0x2e, 0x8d]);

console.log( buf.readDoubleBE(0) );
// Prints: 604.274335557087

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...