TypedArr (TB06)
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.setUint32(0, 0x12345678, true); // true for little-endian
6:
7: // Get the byte array
8: const byteArray = new Uint8Array(buffer);
9: console.log(byteArray); // Output: Uint8Array [ 120, 86, 52, 18 ] (little-endian)
10:
11: // Convert byte array to hex string
12: const hexString = Array.from(byteArray).map(byte => byte.toString(16).padStart(2, '0')).join('');
13: console.log(hexString); // Output: "78563412"
Working with Byte Arrays Uint8Array(4) [ 120, 86, 52, 18 ] 78563412
TypedArr context:
ES6 context:
Binary context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/JavascriptES6/TB06.htm
|
|