如果二进制数据是"4字节单一格式",那意味着什么?以及如何使用JavaScript读取它? [英] What does it mean if binary data is "4 byte single format" and how do I read it in JavaScript?

查看:161
本文介绍了如果二进制数据是"4字节单一格式",那意味着什么?以及如何使用JavaScript读取它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须读取一个据说被编码为4 byte single format的二进制文件,而不必使用二进制数据,我不知道这意味着什么.

I have to read a binary file which is said to be encoded 4 byte single format and never having to work with binary data, I don't know what this means.

我可以通过JavaScript读取带有二进制数据的文件:

I can do this reading a file with binary data in JavaScript:

 d = new FileReader();
 d.onload = function (e) {
   var i, len;
   // grab a "chunk"
   response_buffer = e.target.result.slice(0, 1024);
   view = new DataView(response_buffer);

   for (i = 0, len = response_buffer.byteLength; i < len; i += 1) {
      // hmhm
      console.log(view.getUint8(i));
   }
}
d.readAsArrayBuffer(some_file);

哪个循环从0到1023,我在控制台上获取数字,但是我不知道这是否是我的解码数据:-)

Which runs a loop from 0 to 1023 and I am getting numbers on the console, but I don't know if this is my decoded data :-)

问题:
什么是4字节单格式,如何正确访问数据?

Question:
What is 4 byte single format and how do I access the data correctly? What is the difference between say getUint8() and getint8() or getInt32() in "human understandable language"?

谢谢!

推荐答案

4字节单一格式在计算机科学中不是一个普遍理解的术语.

4 byte single format is not a commonly understood term in computer science.

如果您可以期望文件是一系列单精度浮点数,那么我可能会猜测"4字节单格式"表示单精度浮点,因为每个文件长4个字节.

If you could expect your file to be a series of single precision floating point numbers, then I might guess that "4 byte single format" means single precision floating point because each of those is four bytes long.

您将要使用getFloat32()来解析二进制流中的单精度浮点数.

You will want to use getFloat32() to parse single precision floating point numbers from the binary stream.

如果要用getFloat32()解析1024个数字,则需要1024 * 4字节,并且由于getFloat32()一次处理四个字节,因此每次需要将for循环前进四个字节:

If you want 1024 numbers parsed with getFloat32(), then you need 1024*4 bytes and you need to advance your for loop by four bytes each time since getFloat32() processes four bytes at a time:

d = new FileReader();
 d.onload = function (e) {
   var i, len;
   // grab a "chunk"
   response_buffer = e.target.result.slice(0, 1024 * 4);
   view = new DataView(response_buffer);

   for (i = 0, len = response_buffer.byteLength; i < len; i += 4) {
      // hmhm
      console.log(view.getFloat32(i));
   }
}
d.readAsArrayBuffer(some_file);


另外,请注意,如果您打算在常规网页中使用IE10和IOS 5,则ArrayBuffer不具有.slice()方法.


Also, please note that IE10 and IOS 5 do not have the .slice() method for an ArrayBuffer if you're planning on using this in a general web page.

这篇关于如果二进制数据是"4字节单一格式",那意味着什么?以及如何使用JavaScript读取它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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