在JS中读取/写入浮点字节 [英] Read/Write bytes of float in JS

查看:153
本文介绍了在JS中读取/写入浮点字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以读取JS中的浮点值的字节?我需要的是写一个原始的FLOAT或DOUBLE值成为我需要的二进制格式,那么有什么办法可以得到一个字节接一个字节的IEEE 754表示?同样的问题,当然写。

解决方案

这段代码有帮助吗?

@ Kevin Gadd

  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


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?

@Kevin Gadd:

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中读取/写入浮点字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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