DataView.getUint16()
JavaScript
DataView.getUint16()是dataView中的内置方法。用于在指定位置获取无符号的16位
integer(unsigned short)。
语法
dataview.getUint16(byteoffset)
参数
byteoffset :从读取数据的视图开始处的偏移量(以字节为单位)。
返回值
此方法返回一个无符号的16位整数。
浏览器支持
Chrome |
9 |
Safari |
5.1 |
Firefox |
15 |
Opera |
12.1 |
示例1
<script>
//JavaScript to illustrate dataview.getUint16() method
// creating a ArrayBuffer
var lidihuo = new ArrayBuffer(8);
// creating a view
var arr = new DataView(lidihuo);
// put the value 3.1 at slot 1
arr.setUint16(1,3.1);
document.write("将值3.1转换为:<br><br>");
document.write(arr.getUint16(1));
//expected output: 3
</script>
输出:
将值3.1转换为:
3
示例2
<script>
//JavaScript to illustrate dataview.getUint16() method
// if there is no data to be stored then it returns Nan
// creating a ArrayBuffer
var lidihuo = new ArrayBuffer(8);
// creating a view
var arr = new DataView(lidihuo);
// NO Data
arr.setUint16(1);
document.write("如果没有要存储的数据,则输出将为:<br><br>")
document.write(arr.getUint16(1));
//expected output:0
</script>
输出:
如果没有要存储的数据,则输出将为:
0
示例3
<script>
//JavaScript to illustrate dataview.getUint16() method
// creating a ArrayBuffer
var lidihuo = new ArrayBuffer(8);
// creating a view
var arr = new DataView(lidihuo);
//We can also use math function like Math.PI
arr.setUint16(1,Math.PI);
document.write("将PI值3.14转换为:<br><br>");
document.write(arr.getUint16(1));
//expected output:3
</script>
输出:
将PI值3.14转换为:
3
示例4
<script>
//JavaScript to illustrate dataview.getUint16() method
// creating a ArrayBuffer
var lidihuo = new ArrayBuffer(8);
// creating a view
var arr = new DataView(lidihuo);
// put the value 40 at slot 1
arr.setUint16(1,40);
document.write("如果我们提供一个Integer值,那么Output将为Integer为<br><br>");
document.write(arr.getUint16(1));
//expected output: 40
</script>
输出:
如果我们提供一个Integer值,那么Output将为Integer为
40