TypedArr (TB01)
1: const buffer = new ArrayBuffer(4); // 4 bytes for a 32-bit integer
2: const view = new DataView(buffer);
3:
4: // Write a 32-bit integer in little-endian format
5: view.setInt32(0, 12345, true); // true for little-endian
6:
7: // Read the 32-bit integer in little-endian format
8: const littleEndianValue = view.getInt32(0, true);
9: console.log(littleEndianValue); // Output: 12345
10:
11: // Read the 32-bit integer in big-endian format
12: const bigEndianValue = view.getInt32(0, false);
13: console.log(bigEndianValue); // Output: 875770417 (different due to endianness)
Key Concepts of DataView:
ArrayBuffer:
A fixed-length raw binary data buffer.
It doesn’t directly allow you to manipulate its data; you need a DataView or typed array to access it.
DataView:
Provides a view into an ArrayBuffer to read/write multi-byte data types.
Allows you to specify endianness (little-endian or big-endian) for each operation.
Endianness:
Little-endian: The least significant byte (LSB) is stored at the lowest memory address.
Big-endian: The most significant byte (MSB) is stored at the lowest memory address.
Common Methods:
setInt8, setUint8, setInt16, setUint16, setInt32, setUint32, setFloat32, setFloat64
getInt8, getUint8, getInt16, getUint16, getInt32, getUint32, getFloat32, getFloat64
Writing and Reading a 32-bit Integer
12345
959447040
TypedArr context:
ES6 context:
Binary context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/JavascriptES6/TB01.htm
|
|