JavaScript DataView getUint32() 方法
JavaScript DataView getUint32() 方法用于从此数据视图的指定字节偏移量开始读取 4 个字节数据,并将其解释为 32 位无符号 整数。如果我们不向此方法传递任何参数,它将自动返回存储的值。
如果 byteOffset 参数值正确传递,并且 littleEndian 参数值传递了 0 以外的任何值,它将始终返回此数据视图的最大可能值。
如果 byteOffset 参数值超出此数据视图的边界,它将抛出 'RangeError' 异常。
语法
以下是 JavaScript DataView getUint32() 方法的语法 -
getUint32(byteOffset, littleEndian)
参数
此方法接受两个名为"byteOffset"和"littleEndian",如下所述 -
- byteOffset - DataView 中读取数据的位置。
- littleEndian(可选) - 指示数据是以小端还是大端格式存储。
返回值
此方法返回一个从 0 到 4294967295(含)的整数。
示例 1
以下是 JavaScript DataView getUint32() 方法的基本示例。
<html>
<body>
<script>
//创建数组缓冲区
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);
//让我们使用 setUnit32() 方法设置数据
data_view.setUint32(1, 230);
//使用 getUnit32() 方法
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() 方法存储在此数据视图指定字节偏移量 0 处的 4 字节数据 4294967295。
<html>
<body>
<script>
//创建数组缓冲区
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);
//使用 getUnit32() 方法
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 参数值超出此数据视图的边界,则会抛出 'RangeError' 异常。
<html>
<body>
<script>
//创建数组缓冲区
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);
//使用 getUnit32() 方法
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
如果您向 littleEndian 参数传递除 0 以外的任何值,此方法将始终返回此数据视图的最大值(即 4294967295)。但是,如果使用正确的字节偏移量并将 littleEndian 参数设置为 0,它将返回存储的值。
<html>
<body>
<script>
//创建数组缓冲区
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);
//使用 getUnit32() 方法
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 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

