• JavaScript Video Tutorials

JavaScript DataView getUint32() Method



The JavaScript DataView getUint32() method is used to read 4 bytes data starting at the specified byte offset of this data view and interprets them as a 32-bit unsigned integer. If we do not pass any parameter to this method, it will return the stored value automatically.

If the byteOffset parameter values are passed correctly, and the littleEndian parameter value passes anything other than 0, it will always return the maximum possible value of this data view.

It will throw a 'RangeError' exception if the byteOffset parameter value is outside the bounds of this data view.

Syntax

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

getUint32(byteOffset, littleEndian)

Parameters

This method accepts two parameters named "byteOffset" and "littleEndian", which are described below −

  • byteOffset − The position in the DataView from which to read the data.
  • littleEndian (optional) − It indicates whether the data is stored in little-endian or big-endian format.

Return value

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

Example 1

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

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   const value = 230;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(1, 230);
   //using the getUnit32() method
   document.write("<br>The data_view.getUnit32(1) method returns: ", data_view.getUint32(1));
</script>
</body>
</html>

Output

The above program reads the data and returns as −

The byte offset: 1
The data value: 230
The data_view.getUnit32(1) method returns: 230

Example 2

If we do not pass any parameter to this method, it will automatically return the stored value.

The following is another example of the JavaScript DataView getUint32() method. We use this method to read the 4-byte data 4294967295 stored by the setUnit32() method at the specified byte offset 0 of this data view.

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   const value = 4294967295;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(byteOffset, value);
   //using the getUnit32() method
   try {
      document.write("<br>The data_view.getUnit32() method returns: ", data_view.getUint32());
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

After executing the above program, it will return the stored value as −

The byte offset: 0
The data value: 4294967295
The data_view.getUnit32() method returns: 4294967295

Example 3

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

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   const value = 255;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(byteOffset, value);
   //using the getUnit32() method
   try {
      document.write("<br>The data_view.getUnit32(-1) method returns: ", data_view.getUint32(-1));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Ouput

Once the above program is executed, it will throw a 'RangeError' exception.

The byte offset: 1
The data value: 255
RangeError: Offset is outside the bounds of the DataView

Example 4

If you pass anything other than 0 to the littleEndian parameter, this method will always return the maximum value of this data view (which is 4294967295). However, with the correct byte offset and a littleEndian parameter set to 0, it will return the stored value.

<html>
<body>
<script>
   //creating array buffer
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   const value = 255;
   const littleEndian1 = 0;
   const littleEndian2 = 1;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>The data value: ", value);
   //lets set the data using the setUnit32() method
   data_view.setUint32(byteOffset, value);
   //using the getUnit32() method
   document.write("<br>The littleEndian parameter with a value of 0: ", data_view.getUint32(byteOffset, littleEndian1));
   document.write("<br>The littleEndian parameter with a value of 1: ", data_view.getUint32(byteOffset, littleEndian2));
</script>
</body>
</html>

Output

The above program returns stored value and the default maximum value of this data view as −

The byte offset: 1
The data value: 255
The littleEndian parameter with a value of 0: 255
The littleEndian parameter with a value of 1: 4278190080
Advertisements