JavaScript DataView getUint32() 方法用于读取从此数据视图的指定字节偏移量开始的 4 字节数据,并将其解释为 32 位无符号整数。如果我们不向该方法传递任何参数,它将自动返回存储的值。
如果 byteOffset 参数值传递正确,并且 littleEndian 参数值传递 0 以外的任何内容,它将始终返回此数据视图的最大可能值。
如果 byteOffset 参数值超出此数据视图的边界,它将引发 'RangeError' 异常。
语法
以下是 JavaScript DataView getUint32() 方法的语法 -
getUint32(byteOffset, littleEndian)
参数
此方法接受两个名为 “byteOffset” 和 “littleEndian” 的参数,如下所述 -
- byteOffset - DataView 中要从中读取数据的位置。
- littleEndian(可选)− 它指示数据是以 little-endian 还是 big-endian 格式存储。
返回值
此方法返回一个从 0 到 4294967295(含 0 和 0)的整数。
示例 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 data value: 230
The data_view.getUnit32(1) method returns: 230
示例 2
如果我们没有向该方法传递任何参数,它将自动返回存储的值。
以下是 JavaScript DataView getUint32() 方法的另一个示例。我们使用此方法读取 setUnit32() 方法存储在此数据视图的指定字节偏移量 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 data value: 4294967295
The data_view.getUnit32() method returns: 4294967295
示例 3
如果 byteOffset 参数值超出此数据视图的边界,它将引发 '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 data value: 255
RangeError: Offset is outside the bounds of the DataView
示例 4
如果你向 littleEndian 参数传递 0 以外的任何内容,此方法将始终返回此数据视图的最大值(即 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>
输出
上述程序返回存储值和此数据视图的默认最大值为 -
The data value: 255
The littleEndian parameter with a value of 0: 255
The littleEndian parameter with a value of 1: 4278190080