dataView.getUint8()是dataView中的內置函數,用於在指定位置(即,從dataView的起始字節偏移)獲取無符號的8位整數。
用法:
dataView.getUint8(byteOffset)
參數:它具有參數byteOffset(字節偏移量),該參數以字節為單位偏移,並且它從視圖的開始說起讀取數據的位置。
返回值:它返回無符號的8位整數。
JavaScript代碼顯示此方法的用法原理:
示例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.setUint8(1, 56);
document.write(dataview1.getUint8(1) + "<br>");
</script>
輸出:
56
示例2:
此函數將PI的浮點值從3.14轉換為3
<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 value of PI at slot 1
dataview1.setUint8(1, Math.PI);
document.write(dataview1.getUint8(1) + "<br>");
</script>
輸出:
3
示例3:
如果沒有要存儲的數據,則返回零(0)。
<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.setUint8(1);
document.write(dataview1.getUint8(1) + "<br>");
</script>
輸出:
0
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | dataView.getUint8()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。