当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript DataView.getInt16()用法及代码示例


JavaScript DataView.getInt16() 是 dataView 中的一个内置方法。它用于在指定位置获取带符号的 16 位整数(短)数。

注意:16 位整数值的范围是从 0 到 65,535(无符号整数值)和从 ?32,768 到 32,767(有符号整数值)。

用法

dataview.getInt16(byteOffset)

参数

byteoffset: 从读取数据的视图开始的偏移量,以字节为单位。

返回值

此方法返回一个带符号的 16 位整数。

浏览器支持

Chrome 9
Safari 5.1
Firefox 15
Opera 12.1

例子1

<script>
//JavaScript to illustrate dataview.getInt16() method
// creating a ArrayBuffer 
var JavaTpoint = new ArrayBuffer(8);
// creating a view
var arr = new DataView(JavaTpoint);
// put the value 3.1 at slot 1
document.write("If we give a float value then output will be an Integer<br><br>");
arr.setInt16(1,3.1);
document.write(arr.getInt16(1));
//expected output:3
</script>

输出:

If we give a float value then output will be an Integer

3

例子2

<script>
//JavaScript to illustrate dataview.getInt16() method
// if there is no data to be stored then it returns Nan
// creating a ArrayBuffer 
var JavaTpoint = new ArrayBuffer(8);
// creating a view
var arr = new DataView(JavaTpoint);
// NO Data
arr.getInt16(1);
document.write(" If  there is no data to be stored, then Output will Be:<br><br>")
document.write(arr.getInt16(1));
//expected output:0
</script>

输出:

If there is no data to be stored, then Output will Be:

0

例子3

<script>
//JavaScript to illustrate dataview.getInt16() method
// creating a ArrayBuffer 
var JavaTpoint = new ArrayBuffer(8);
// creating a view
var arr = new DataView(JavaTpoint);
//We can also use math function like Math.PI
document.write("PI value will be <br><br>");
arr.setInt16(1,Math.PI);
document.write(arr.getInt16(1));
//expected output:3
</script>

输出:

PI value will be 

3

示例 4

<script>
//JavaScript to illustrate dataview.getInt16() method
// creating a ArrayBuffer 
var JavaTpoint = new ArrayBuffer(8);
// creating a view
var arr = new DataView(JavaTpoint);
// put the value 7 at slot 1
arr.setInt16(1,7);
document.write("If we provide Integer value then output will be<br><br>")
document.write(arr.getInt16(1));
//expected output:7
</script>

输出:

If we provide Integer value then output will be.

7




相关用法


注:本文由纯净天空筛选整理自 JavaScript DataView.getInt16() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。