Skip to content
Related Articles

Related Articles

JavaScript dataView.getInt8() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 10 Oct, 2021

Below is the example of the dataView.getInt8()() Method. 
 

  • Example: 
     

javascript




<script>
var buffer = new ArrayBuffer(16);
var dataview1 = new DataView(buffer, 0, 4);
dataview1.setInt8(0, 12);
document.write(dataview1.getInt8(0) + "<br>");
</script>

  • Output: 
     
12

The dataView.getInt8() is a method in dataView which is used to get an 8-bit integer in the byte at the specified location from the start of the dataView.
Syntax: 
 

dataview.getInt8(byteOffset)

Parameters: It has parameter byteOffset which is offset in a byte and it says from the start of the view were to read the data.
Return value: It returns 8-bit signed integer value.
Example 1: 
 

Input: dataview1.setInt8(0, 123); 
       document.write(dataview1.getInt8(0)); 
Output: 123

Example 2: 
 

Input: dataview.setInt8(3, 45); 
       document.write(dataview.getInt8(3)); 
Output: 45

JavaScript code to show the working of this method: 
Code #1: 
 

javascript




<script>
 
// Create buffer
var buffer = new ArrayBuffer(16);
 
// Create one view
var dataview1 = new DataView(buffer, 0, 4);
 
// put the data at slot 0
dataview1.setInt8(0, 123);
document.write(dataview1.getInt8(0) + "<br>");
 
// create another view
var dataview2 = new DataView(buffer, 1, 2);
 
// put data at slot 1
dataview2.setInt8(1, 45);
document.write(dataview2.getInt8(1));
 
< /script>                               

Output: 
 

123
45

Code #2: 
 

javascript




<script>
 
// Create buffer
var buffer = new ArrayBuffer(16);
var dataview = new DataView(buffer, 0, 10);
 
// put data at slots
dataview.setInt8(0, 12);
dataview.setInt8(1, 23);
dataview.setInt8(2, 34);
dataview.setInt8(3, 45);
dataview.setInt8(4, 67);
dataview.setInt8(5, 78);
dataview.setInt8(6, 89);
dataview.setInt8(7, 90);
dataview.setInt8(8, 123);
 
// print the value using getInt8 method that
// prints the first 8 int
document.write(dataview.getInt8(0) + "<br>");
document.write(dataview.getInt8(1) + "<br>");
document.write(dataview.getInt8(2) + "<br>");
document.write(dataview.getInt8(3) + "<br>");
document.write(dataview.getInt8(4) + "<br>");
document.write(dataview.getInt8(5) + "<br>");
document.write(dataview.getInt8(6) + "<br>");
document.write(dataview.getInt8(7) + "<br>");
document.write(dataview.getInt8(8));
 
< /script>                   

Output: 
 

12
23
34
45
67
78
89
90
123

Supported Browsers: 
 

  • Google Chrome 9 and above
  • Edge 12 and above
  • Firefox 15 and above
  • Internet Explorer 10 and above
  • Opera 12.1 and above
  • Safari 5.1 and above

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!