當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaScript DataView.getUint8()用法及代碼示例


JavaScript DataView.getUint8() 是 dataView 中的一個內置方法。它用於獲取指定位置的無符號 8 位整數(無符號字節)數。

用法

dataview.getUint8(byteoffset)

參數

byteoffset: 從讀取數據的視圖開始的偏移量,以字節為單位。

返回值

此方法返回一個無符號的 8 位整數。

瀏覽器支持

Chrome 9
Safari 5.1
Firefox 15
Opera 12.1

例子1

<script>
//JavaScript to illustrate dataview.getUint8() method
// creating a ArrayBuffer 
var JavaTpoint = new ArrayBuffer(8);
// creating a view
var arr = new DataView(JavaTpoint);
// put the value 33.4 at slot 1
arr.setUint8(1,33.4);
document.write("Convert the Float value 33.4 to:<br><br>");
document.write(arr.getUint8(1));
//expected output:33
</script>

輸出:

Convert the Float value 33.4 to:

33

例子2

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

輸出:

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

0

例子3

<script>
//JavaScript to illustrate dataview.getUint8() method
// creating a ArrayBuffer 
var JavaTpoint = new ArrayBuffer(10);
// creating a view
var arr = new DataView(JavaTpoint);
//We can also use math function like Math.PI
arr.setUint8(1,Math.PI);
document.write("Convert the PI value 3.14 to <br><br>");
document.write(arr.getUint8(1));
//expected output:3 
</script>

輸出:

Convert the PI value 3.14 to 

3

示例 4

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

輸出:

If we give an Integer value then Output will be Integer

67




相關用法


注:本文由純淨天空篩選整理自 JavaScript DataView.getUint8() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。