JavaScript DataView getBigInt64() 方法



JavaScript DataView 的 getBigInt64() 方法用於從 DataView 的指定位元組偏移量開始檢索 8 位元組整數的值。它將這些位元組解碼為 64 位有符號整數。此外,您可以從 DataView 範圍內的任何偏移量檢索多個位元組。

如果byteOffset 引數的值超出此 DataView 的範圍,則嘗試使用 getBigInt64() 方法檢索資料將丟擲'RangeError' 異常。

語法

以下是 JavaScript DataView getBigInt64() 方法的語法:

getBigInt64(byteOffset, littleEndian)

引數

此方法接受兩個名為“byteOffset”和“littleEndian”的引數,如下所述:

  • byteOffset - 從 DataView 中讀取資料的起始位置。
  • littleEndian - 指示資料是以小端序還是大端序儲存。

返回值

此方法返回一個 BigInt,範圍為 -263263-1(包含)。

示例 1

以下是 JavaScript DataView getBigInt64() 方法的基本示例。

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   //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);
   //using the getBigInt64() method
   document.write("<br>The store value: ", data_view.getBigInt64(byteOffset));
</script>
</body>
</html>

輸出

上述程式返回儲存的值。

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

示例 2

如果 byteOffset 引數的值超出此資料檢視的範圍,它將丟擲'RangeError' 異常。

<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 getBigInt64() method
     document.write("<br>The store value: ", data_view.getBigInt64(-1));
   } catch (error) {
     document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

執行上述程式後,它將丟擲以下異常:

The byte offset: 1
Value: 9223372036854775807
RangeError: Offset is outside the bounds of the DataView
廣告