DataView instance method getUint16() in Javascript
The DataView instance method getUint16() used to retrieve a unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the DataView. It returns unsigned 16-bit integer number from the specified offset.
Syntax
 1 <object>.getUint16(<byteOffset>, <littleEndian>)
object : a DataView object to retrieve unsigned int 16 bit value
byteOffset : an offset (in bytes) from the start of the view to read the data from DataView
littleEndian : It is an optional, if specified, it indicates whether the 16-bit integer is stored in little- or big-endian format. If false or undefined, a big-endian value is read.
Instance method getUint16()
 1 const buffer = new ArrayBuffer(16);
 2 
 3 const view = new DataView(buffer);
 4 view.setUint16(1, 65535); // setting max value at offset 1
 5 
 6 let res = view.getUint16(1)
 7 console.log("A resulting value :", res);
In the above example, a buffer object is created by specifying length. A DataView object is created by specifying a buffer object and setUint16() method is called by specifying an offset 1 and 65535.
An instance method getUint16() method is called by providing an offset value 1 as value stored at offset 1 by setter method and printed on console.
Output
 1 A resulting value : 65535

Example with offset

Example
 1 const buffer = new ArrayBuffer(16);
 2 const dv = new DataView(buffer);
 3 
 4 // setting value at 0, 2 offset
 5 dv.setUint16(0, 123);
 6 dv.setUint16(2, 514);
 7 
 8 let res = dv.getUint16(0);
 9 console.log("A resulting value :", res);
 10 
 11 let res1 = dv.getUint16(2);
 12 console.log("A resulting value :", res1);
In the above example, a DataView object created by specifying a buffer which created by specifying a length 16. A getUint16() called by specifying a byte offset 0 and 2, a byte offset must be multiple of 2. As Uinteger 16, requires 2 bytes to store value. A value is set and retrieve using setter and getter method.
Output
 1 A resulting value : 123
 2 A resulting value : 514
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us