JavaScript DataView getUint32() 方法



JavaScript DataView 的getUint32()方法用於讀取從DataView指定位元組偏移量開始的4位元組資料,並將其解釋為一個32位無符號整數。如果我們不向此方法傳遞任何引數,它將自動返回儲存的值。

如果byteOffset引數值正確傳遞,並且littleEndian引數值傳遞的值不是0,它將始終返回此DataView的最大可能值。

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

語法

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

getUint32(byteOffset, littleEndian)

引數

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

  • byteOffset - 從中讀取資料的 DataView 中的位置。
  • littleEndian (可選) - 指示資料是按小端序還是大端序儲存。

返回值

此方法返回一個介於04294967295(含)之間的整數。

示例 1

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

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   const value = 230;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(1, 230);
   //using the getUnit32() method
   document.write("<br>The data_view.getUnit32(1) method returns: ", data_view.getUint32(1));
</script>
</body>
</html>

輸出

上述程式讀取資料並返回如下結果:

The byte offset: 1
The data value: 230
The data_view.getUnit32(1) method returns: 230

示例 2

如果我們向此方法傳遞任何引數,它將自動返回儲存的值。

以下是 JavaScript DataView getUint32() 方法的另一個示例。我們使用此方法讀取由 setUnit32() 方法在此DataView的指定位元組偏移量 0 處儲存的 4 位元組資料 4294967295

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   const value = 4294967295;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(byteOffset, value);
   //using the getUnit32() method
   try {
      document.write("<br>The data_view.getUnit32() method returns: ", data_view.getUint32());
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

執行上述程式後,它將返回儲存的值:

The byte offset: 0
The data value: 4294967295
The data_view.getUnit32() method returns: 4294967295

示例 3

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

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   const value = 255;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(byteOffset, value);
   //using the getUnit32() method
   try {
      document.write("<br>The data_view.getUnit32(-1) method returns: ", data_view.getUint32(-1));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

執行上述程式後,它將丟擲“RangeError”異常。

The byte offset: 1
The data value: 255
RangeError: Offset is outside the bounds of the DataView

示例 4

如果將除0以外的任何值傳遞給littleEndian引數,此方法將始終返回此DataView的最大值(即4294967295)。但是,如果位元組偏移量正確,並且 littleEndian 引數設定為 0,則它將返回儲存的值。

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   const value = 255;
   const littleEndian1 = 0;
   const littleEndian2 = 1;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(byteOffset, value);
   //using the getUnit32() method
   document.write("<br>The littleEndian parameter with a value of 0: ", data_view.getUint32(byteOffset, littleEndian1));
   document.write("<br>The littleEndian parameter with a value of 1: ", data_view.getUint32(byteOffset, littleEndian2));
</script>
</body>
</html>

輸出

上述程式返回儲存的值和此DataView的預設最大值:

The byte offset: 1
The data value: 255
The littleEndian parameter with a value of 0: 255
The littleEndian parameter with a value of 1: 4278190080
廣告