2

I'm trying to convert an array buffer to a Uint8Array and it alwasy comes out as zeros.

buff = new ArrayBuffer(4)
buff[0] = 10
buff[1] = 10
buff[2] = 10
buff[3] = 10
console.log(new Uint8Array(buff))
 -> Uint8Array(4) [0, 0, 0, 0]

1 Answer 1

2

Quoting the docs at ArrayBuffer

You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.

Sign up to request clarification or add additional context in comments.

3 Comments

So let's say the array buffer is provided to me via xmlhttp.response, how would I convert it to a uint8array?
@SanchkeDellowar The conversion you do is valid, but you cannot alter the values of the buffer directly.
@SanchkeDellowar although keep in mind that it is not really a conversion, it is merely a view-as

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.