简体   繁体   English

如何将二进制数据读取到Javascript中的字节数组?

[英]How do I read binary data to a byte array in Javascript?

I want to read a binary file in JavaScript that would be gotten through XMLHttpRequest and be able to manipulate that data. 我想用XMLHttpRequest读取JavaScript中的一个二进制文件,并能够处理该数据。 From my researching I discovered this method of reading a binary file data into an array 通过研究,我发现了将二进制文件数据读入数组的方法

var xhr = new XMLHttpRequest();
xhr.open('GET', '/binary_And_Ascii_File.obj', true);

xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
  var uInt8Array = new Uint8Array(this.response);
};

How do I convert this binary data array to a human-readable-string? 如何将该二进制数据数组转换为人类可读的字符串?

I'm sure you will find this helpful: http://jsdo.it/tsmallfield/uint8array . 我相信您会发现这很有用: http : //jsdo.it/tsmallfield/uint8array

Click on javascript tab. 点击javascript标签。 There will appear the code to convert the Uint8Array in a string. 将会出现将Uint8Array转换为字符串的代码。 The author shows 2 method: 作者展示了两种方法:

  • The first is about creating a view. 首先是关于创建视图。
  • The second offsetting bytes. 第二个偏移量字节。

EDIT: report the code for completeness 编辑:报告代码的完整性

var buffer = new ArrayBuffer( res.length ), // res is this.response in your case
    view   = new Uint8Array( buffer ),
    len    = view.length,
    fromCharCode = String.fromCharCode,
    i, s, str;    

/**
 *  1) 8bitの配列に入れて上位ビットけずる
 */
str = "";

for ( i = len; i--; ) {
  view[i] = res[i].charCodeAt(0);
}

for ( i = 0; i < len; ++i ) {
  str += fromCharCode( view[i] );
}    

/**
 *  2) & 0xff で上位ビットけずる
 */
str = "";

for ( i = 0; i < len; ++i ) {
  str += fromCharCode( res[i].charCodeAt(0) & 0xff );
}
function load_binary_resource(url) {
  var byteArray = [];
  var req = new XMLHttpRequest();
  req.open('GET', url, false);
  req.overrideMimeType('text\/plain; charset=x-user-defined');
  req.send(null);
  if (req.status != 200) return byteArray;
  for (var i = 0; i < req.responseText.length; ++i) {
    byteArray.push(req.responseText.charCodeAt(i) & 0xff)
  }
  return byteArray;
}

See https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data for more details 有关更多详细信息,请参见https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果二进制数据是“ 4字节单一格式”,那是什么意思?如何用JavaScript读取它? - What does it mean if binary data is “4 byte single format” and how do I read it in JavaScript? 如何使用 javascript 逐字节读取二进制文件? - How to read binary file byte by byte using javascript? Javascript-如何将图像数据(字节数组)添加到多部分POST请求中 - Javascript - How do I add image data (byte array) to multipart POST request 如何在javascript中将字节数组转换为图像 - How do i convert byte array to image in javascript 如何从JavaScript访问COM字节数组 - How do I access a COM byte array from JavaScript 在JavaScript中将字节数组转换为二进制 - Convert Byte array to Binary in JavaScript 如何从 javascript 中的任意字节开始从字节数组转换为不同的数字类型 - How do I convert from a byte array to different number types starting at arbitrary byte starts in javascript 如何将二进制数据从C ++发送到Javascript - How do i send binary data from C++ to Javascript 如何使用Javascript和XMLHttpRequest加载二进制图像数据? - How do I load binary image data using Javascript and XMLHttpRequest? 如何从WebSocket的二进制数据流填充数组 - How do I fill an array with binary data streaming from websocket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM