dataView.getInt16()是dataView中的內置函數,用於在指定位置(即,距dataView起始字節偏移)獲取16位整數。 16位整數值的範圍從0到65,535(無符號),從?32,768到32,767(帶符號整數)。
用法:
dataview.getInt16(byteOffset)
參數:它具有參數byteOffset,該參數以字節為單位偏移,它表示從視圖的start(start)讀取數據的位置。
返回值:它返回16位帶符號整數值。
示例1:
<script>
// Creating buffer with size in byte
var buffer = new ArrayBuffer(20);
// Creating a view
var dataview1 = new DataView(buffer, 0, 10);
// put the data 56 at slot 1
dataview1.setInt16(1, 56);
document.write(dataview1.getInt16(1) + "<br>");
</script>
輸出:
56
示例2:
從這裏可以看出,當將此函數的浮點數賦給該函數時,該函數不獲取浮點數,然後將該值轉換為整數值。
<script>
// Creating buffer with size in byte
var buffer = new ArrayBuffer(20);
// Creating a view with slot from o to 10
var dataview1 = new DataView(buffer, 0, 10);
// put the 4.5 at slot 1
dataview1.setInt16(1, 4.5);
document.write(dataview1.getInt16(1) + "<br>");
</script>
輸出:
4
示例3:
如果沒有要存儲的數據,則返回NaN,即不是數字。
<script>
// Creating buffer with size in byte
var buffer = new ArrayBuffer(20);
// Creating a view
var dataview1 = new DataView(buffer, 0, 10);
// putting no data at slot 1
dataview1.setInt16(1);
document.write(dataview1.getInt16(1) + "<br>");
</script>
輸出:
0
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | dataView.getInt16()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。