(ES6) ES6 (2016)

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:




Binary context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/TB07.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>