Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

JavaScript DataView getUint8() Method



The JavaScript DataView getUint8() method is used to retrieve a 1-byte data value at the specified byte offset of this DataView and interprets it as an 8-bit unsigned integer. If no number (value) is found at the specified byteOffset, it will return the number value as 0.

If the byteOffset is outside the bounds of this DataView, invoking the getUint8(-1) method will throw a 'RangeError exception.

Syntax

Following is the syntax of the JavaScript DataView getUint8() method −

getUint8(byteOffset)

Parameters

This method accepts a parameter named 'byteOffset', which is described below −

  • byteOffset − The position in the DataView from which to read the data.

Return value

This method returns an integer from 0 to 255, inclusive.

Example 1

The following is the basic example of the JavaScript DataView getUint8() method.

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(8);
   const data_view = new DataView(buffer);
   const number = 20;
   const byteOffset = 0;
   document.write("Number value: ", number);
   document.write("<br>The byteOffset value: ", byteOffset);
   //using the setUnit8() to store number
   data_view.setUint8(byteOffset, number);
   //using getUnit8() method to read the stored value
   document.write("<br>The data_view.getUint8(0) method returns: ", data_view.getUint8(0));
</script>
</body>
</html>

Output

The above program read 1 byte and returns 20 −

Number value: 20
The byteOffset value: 0
The data_view.getUint8(0) method returns: 20

Example 2

Ifno numberis found at the specified byteOffset 2, the getUint8() method will return0.

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(8);
   const data_view = new DataView(buffer);
   const number = 225;
   const byteOffset = 2;
   document.write("Number value: ", number);
   document.write("<br>The byteOffset value: ", byteOffset);
   //using the setUnit8() to store number
   data_view.setUint8(byteOffset, number);
   //using getUnit8() method to read the stored value
   document.write("<br>The data_view.getUint8(1) method returns: ", data_view.getUint8(1));
</script>
</body>
</html>

Output

Once the above program is executed, it will return the data value as 0.

Number value: 225
The byteOffset value: 2
The data_view.getUint8(1) method returns: 0

Example 3

If the byteOffset parameter value is outside the bound of the data view, it will throw a 'RangeError' exception.

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(8);
   const data_view = new DataView(buffer);
   const number = 10;
   const byteOffset = 0;
   document.write("Number value: ", number);
   document.write("<br>The byteOffset value: ", byteOffset);
   //using the setUnit8() to store number
   data_view.setUint8(byteOffset, number);
   //using getUnit8() method to read the stored value
   try {
      document.write(data_view.getUint8(-1));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

The program mentioned above throws a 'RangeError' exception after execution.

Number value: 10
The byteOffset value: 0
RangeError: Offset is outside the bounds of the DataView
Advertisements