TypedArr (TB07)
1: // 1. Reading and Writing Different Data Types:
2:
3: const buffer = new ArrayBuffer(16); // Allocate 16 bytes
4: const view = new DataView(buffer);
5:
6: view.setInt8(0, 42); // Write an 8-bit integer at offset 0.
7: view.setInt16(1, 1000, true); // Write a 16-bit integer (little-endian).
8: view.setInt32(3, -256000, false); // Write a 32-bit integer (big-endian)
9: view.setFloat32(7, 3.14159, true); // Write a 32-bit float (little-endian)
10:
11: console.log(view.getInt8(0)); // Read the 8-bit integer at offset 0. Output: 42
12: console.log(view.getInt16(1, true)); // Read the 16-bit integer. Output: 1000
13: console.log(view.getInt32(3, false)); // Output: -256000
14: console.log(view.getFloat32(7, true)); // Output: 3.14159
15:
16: //You need to allocate enough space in your ArrayBuffer to accommodate the BigInt. Since a BigInt64 takes 8 bytes, and you want to write it starting at an offset of 11 bytes, your total buffer size should be at least 11 + 8 = 19 bytes.
17: const buffer1 = new ArrayBuffer(19);
18: const view1 = new DataView(buffer1);
19:
20: view1.setBigInt64(11, 1234567890123456789n, true); // Write a 64-bit BigInt (little-endian)
21: console.log(view1.getBigInt64(11, true)); // Output: 1234567890123456789n
22:
DataView provides a flexible, low-level way to interact with binary data in JavaScript's ArrayBuffer. It lets you read and write data of various types (integers, floats, strings) at specific byte offsets within the buffer, regardless of the underlying Typed Array view. This control over byte representation and endianness is essential for tasks like parsing binary files, network protocols, or handling data where precise byte layouts matter. 42 1000 -256000 3.141590118408203 1234567890123456789n
TypedArr context:
ES6 context:
- (2024) Notes about JS Closures. #ES6
- (2024) Notes about Javascript asynchronous programming. #ES6
- (2022) Modern Javascript books #ES6 #Doc
- (2021) JS learning start point #ES6
- (2021) Maximilian Schwarzmüller Javascript lecture #ES6
- (2021) Javascript interview question from Happy Rawat #ES6
- (2021) Javascript tests #ES6
- (2016) New unique features of Javascript (updated). #ES6
Binary context:
- (2019) How to change image Exif metadata #Binary
- (2018) З'ясування алгоритмів оновлення Documat-CD SEB. #Binary
- (2016) JS6 Typed Array #ES6 #Binary
- (2015) Сховище графіки на SQL FileStream та канал браузеру multipart/form-data. #Sql #WebServiceClient #Binary
- (2012) Робота з байтами у VB.NET - ChrW, BitConverter.ToInt32, Convert.ToInt32, Byte.Parse, ToString("X2") / AllowHexSpecifier, GetBytes/GetString, New Byte(N) {}, UInt32 = &H33 #Binary #NetCommon
- (2005) DumpExe - утилитка дампирования структуры NET-сборок с открытым исходным текстом. #Binary #WinDesktop
- (2003) DVD - формат и обзор техник работы с ним. #Binary
- (2002) PE - формат исполняемого файла. #Binary
- (2002) FRX-парсер - моя утилитка для выкусывания рисунков из шестерочного FRX-файла. #WinDesktop #Vb6 #Binary
Comments (
)

Link to this page:
http://www.vb-net.com/JavascriptES6/TB07.htm
|