TypedArr (TB08)
1: // Assume 'buffer' contains the binary data of a file (implementation for receiving data depends on specific context)
2: const buffer = new ArrayBuffer(20); // For example.
3: const view = new DataView(buffer);
4:
5: view.setUint16(0, 0x1234, true); // 16-bit integer at offset 0
6: view.setUint32(2, 0x56789ABC, true); // 32-bit integer at offset 2
7:
8: const header = view.getUint32(0); // Read a 32-bit unsigned integer header at offset 0
9: const version = view.getUint8(4); // Read an 8-bit unsigned integer version at offset 4
10: const timestamp = view.getFloat64(5, false); // Read a 64-bit float timestamp (big-endian)
11:
12: console.log (header,version,timestamp)
Parsing a Binary File This code simulates reading a header, version number, and a timestamp at different places in a binary file. The order of bytes (endianness) during read operations must match the endianness of how these values are stored in the binary format. 873643162 120 1.8347988927920572e+106
TypedArr context:
ES6 context:
Binary context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/JavascriptES6/TB08.htm
|
|