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

JavaScript DataView setUint8() Method



TheJavaScript DataView setUint8()method stores an 8-bit unsignedinteger (a byte) at the specified byte offset from the start of the DataView.

If the specified byteOffset is outside the bounds of the DataView, the method throws a'RangeError'exception.

Syntax

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

setUint8(byteOffset, value)

Parameters

This method accepts two parameters named 'byteOffset' and 'value', which are described below −

  • byteOffset − It is the position in the DataView where the byte will be stored.
  • value − An 8-bit unsignedinteger value needs to be stored.

Return value

This method returnsundefined, as it only stores the byte value.

Example 1

The following is the basic example of the JavaScript DataView setUnit8() 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() method
   data_view.setUint8(byteOffset, number);
   document.write("<br>Store value: ", data_view.getUint8());
</script>
</body>
</html>

Output

The above program stores the number and displays it as −

Number value: 20
The byteOffset value: 0
Store value: 20

Example 2

The following is another example of the JavaScript DataViewsetUint8()method. We use this method to store the unsigned 8-bit integer value 50 at the specified byte offset position 2.

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(8);
   const data_view = new DataView(buffer);
   const number = 50;
   const byteOffset = 2;
   document.write("Number value: ", number);
   document.write("<br>The byteOffset value: ", byteOffset);
   //using the setUnit8() method
   data_view.setUint8(byteOffset, number);
   document.write("<br>Store value: ", data_view.getUint8(2));
</script>
</body>
</html>

Output

Once the above program is executed, it stores the number 50 and displays it as −

Number value: 50
The byteOffset value: 2
Store value: 50

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 an array buffer
   const buffer = new ArrayBuffer(8);
   const data_view = new DataView(buffer);
   const number = 200;
   const byteOffset = 9;
   document.write("Number value: ", number);
   document.write("<br>The byteOffset: ", byteOffset);
   try {
      //using the setUnit8() method
      data_view.setUint8(9, 200);
      document.write(dv.getUint8(0));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

After executing the above program, it throws a 'RangeError' exception.

Number value: 200
The byteOffset: 9
RangeError: Offset is outside the bounds of the DataView
Advertisements