简体   繁体   English

Uint8Array 到 ArrayBuffer

[英]Uint8Array to ArrayBuffer

So I have a ArrayBuffer which is of the file contents of a file which I read with the new HTML5 file reader as ArrayBuffer(), and I can convert the ArrayBuffer to Uint8Array by doing the following.所以我有一个 ArrayBuffer,它是我用新的 HTML5 文件阅读器作为 ArrayBuffer() 读取的文件的文件内容,我可以通过执行以下操作将 ArrayBuffer 转换为 Uint8Array。

//ab = established and defined ArrayBuffer

var foobar = new Uint8Array([ab]);

//var reversed = reverseUint8Array(foobar); 

//reversed should equal ab 

How do I reverse that last process back into ab?我如何将最后一个过程反转回 ab?

Here is the kind of output I am getting after decryption: http://prntscr.com/b3zlxr这是我解密后得到的输出类型: http : //prntscr.com/b3zlxr

What kind of format is this, and how do I get it into blob?这是一种什么样的格式,我如何把它变成 blob?

I found a more simple method to get the ArrayBuffer of Uint8Array.我找到了一个更简单的方法来获取Uint8Array的ArrayBuffer。

var arrayBuffer = foobar.buffer; 

just this!只是这个! And it works for me!它对我有用!

With kuokongqingyun option, it will not always work, because the buffer may be bigger than the data view.使用 kuokongqingyun 选项,它不会总是有效,因为缓冲区可能比数据视图大。

See this example:看这个例子:

let data = Uint8Array.from([1,2,3,4])
var foobar = data.subarray(0,2)
var arrayBuffer = foobar.buffer; 

console.log(new Uint8Array(arrayBuffer)) // will print [1,2,3,4] but we want [1,2]

When using array.buffer it's important use byteOffset and byteLength to compute the actual data当使用array.buffer它的重要用途byteOffsetbyteLength来计算实际数据

let data = Uint8Array.from([1,2,3,4])
var foobar = data.subarray(0,2)
var arrayBuffer = foobar.buffer.slice(foobar.byteOffset, foobar.byteLength + foobar.byteOffset); 

console.log(new Uint8Array(arrayBuffer)) // OK it now prints [1,2]

So here is the function you need:所以这是你需要的功能:

function typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
    return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}

If you need to convert it back into an ArrayBuffer() , you need to add each value manually.如果需要将其转换回ArrayBuffer() ,则需要手动添加每个值。

That means, you need to cycle through each one of them, one by one.这意味着,您需要一个一个地循环浏览它们中的每一个。

Probably the fastest way will be like this:最快的方法可能是这样的:

var buffer = new ArrayBuffer(foobar.length);

foobar.map(function(value, i){buffer[i] = value});

Some answers here will only work if the internal buffer of your Uint8Array has no offset.这里的一些答案只有在 Uint8Array 的内部缓冲区没有偏移时才有效。 The answers using .slice() will make an expensive memory copy that is not always needed.使用 .slice() 的答案将制作一个并不总是需要的昂贵的内存副本。

The easiest and most efficient way is like this :最简单有效的方法是这样的:

var buffer = new ArrayBuffer(foobar.buffer, foobar.byteOffset, foobar.byteLength);

Example :例子 :

let data = Uint8Array.from([1,2,3,4])
let foobar = data.subarray(1,3) // [2, 3]

console.log(new Uint8Array(foobar.buffer, foobar.bytesOffset, foobar.byteLength).toString()) // will print [2, 3]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM