JavaScript DataView getBigUnit64() Method



The JavaScript DataView getBigUint64() method is used to retrieve 8-byte data segments starting from a specified byte offset of this DataView. Later on, it decodes them as 64-bit unsigned integers. You can retrieve multiple bytes from any offset within the bounds of the DataView.

This method throws a 'RangeError' exception if you try to read data from a position that exceeds the valid bounds of the DataView.

Syntax

Following is the syntax of the JavaScript DataView getBigUnit64() method −

getBigUnit64(byteOffset, littleEndian)

Parameters

This method accepts two parameters named 'byteOffset' and 'littleEndian', which are described below −

  • byteOffset − The position in the DataView from which to read the data.
  • littleEndian − It indicates whether the data is stored in little-endian or big-endian format.

Return value

This method returns a BigInt in the range from 0 to 264 - 1, inclusive.

Example 1

The following program demonstrates the usage of the JavaScript DataView getBigUint64() method.

<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const byteOffset = 0; //find the highest possible BigInt value that fits for the big unsigned integer const value = 2n ** (64n - 1n) - 1n; document.write("The byte offset: ", byteOffset); document.write("<br>Value: ", value); //using the setBigUnit64() method to store value data_view.setBigUint64(byteOffset, value); //using the getBigUnit64() method document.write("<br>The stored value: ", data_view.getBigUint64(byteOffset)); </script> </body> </html>

Output

The above program returns the stored value.

The byte offset: 0
Value: 9223372036854775807
The stored value: 9223372036854775807

Example 2

The following is another example of the setBigUnit64() method. We use this method to retrieve an 8-byte data segment of the data view from a specified byte offset 1.

<html> <body> <script> const { buffer } = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); const data_view = new DataView(buffer); const byteOffset = 1; document.write("The byte offset: ", byteOffset); document.write("<br>The data_view.getBigUnit64(1) method returns: ", data_view.getBigUint64(byteOffset)); </script> </body> </html>

Output

After executing the program mentioned above, it will return an 8-byte data segment as −

The byte offset: 1
The data_view.getBigUnit64(1) method returns: 72623859790382856

Example 3

If the value of the byteOffset parameter falls outside the bounds of this data view, it will throw a 'RangeError' exception.

<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const byteOffset = 1; //find the highest possible BigInt value const value = 2n ** (64n - 1n) - 1n; document.write("The byte offset: ", byteOffset); document.write("<br>Value: ", value); //storing the value data_view.setBigInt64(byteOffset, value); try { //using the getBigUnit64() method document.write("<br>The store value: ", data_view.getBigUnit64(-1)); } catch (error) { document.write("<br>", error); } </script> </body> </html>

Output

Once the above program is executed, it will throw an exception as −

The byte offset: 1
Value: 9223372036854775807
TypeError: data_view.getBigUnit64 is not a function
Advertisements