JavaScript -
Typed arrays
It has become clear that there are times when it would be helpful for JavaScript code to be able to quickly and easily manipulate raw binary data.In the past, this had to be simulated by treating the raw data as a string and using the charCodeAt() method to read the bytes from the data buffer.
Consider this C structure:
1 2 3 4 5 |
|
You can access a buffer containing data in this format like this:
1 2 3 4 5 |
|
ArrayBuffer
The ArrayBuffer is a data type that is used to represent a generic, fixed-length binary data buffer. You can’t directly manipulate the contents of an ArrayBuffer; instead, you create an ArrayBufferView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
1
|
|
DataView
An ArrayBuffer is a useful object for representing an arbitrary chunk of data. In many cases, such data will be read from disk or from the network, and will not follow the alignment restrictions that are imposed on the Typed Array Views described earlier. In addition, the data will often be heterogeneous in nature and have a defined byte order. The DataView view provides a low-level interface for reading such data from and writing it to an ArrayBuffer.
1 2 3 4 5 6 7 8 |
|
Trick to check the Endian type of architecture your script is running:
1 2 3 4 5 |
|
Reference:
DataView:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays/DataView ArrayBuffer:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer Typed Array Specification:http://www.khronos.org/registry/typedarray/specs/latest/