This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

Int8Array

Summary

A typed array of 8-bit integer values. The contents are initialized to 0. If the requested number of bytes could not be allocated an exception is raised.

Syntax

int8Array = new Int8Array( length );

intArray = new IntArray( array );

intArray = new IntArray( buffer , byteOffset , length );
int8Array
Required. The variable name to which the Int8Array object is assigned.
length
Specifies the length (in bytes) of the array.
array
The array (or typed array) that is contained in this array… The contents are initialized to the contents of the given array or typed array, with each element converted to the Int8 type.
buffer
The ArrayBuffer that the Int8Array represents.
byteOffset
Optional. Specifies the offset in bytes from the start of the buffer at which the Int8Array should begin.
length
The length of the array.

Examples

The following example shows how to use an Int8Array object to process the binary data acquired from an XmlHttpRequest:

var req = new XMLHttpRequest();
     req.open('GET', "http://www.example.com");
     req.responseType = "arraybuffer";
     req.send();

     req.onreadystatechange = function () {
         if (req.readyState === 4) {
             var buffer = req.response;
             var dataview = new DataView(buffer);
             var ints = new Int8Array(buffer.byteLength);
             for (var i = 0; i < ints.length; i++) {
                 ints[i] = dataview.getInt8(i);
             }
         alert(ints[10]);
         }
     }

Constants

The following table lists the constants of the Int8Array object.

ConstantDescription
BYTES_PER_ELEMENT ConstantThe size in bytes of each element in the array.

Properties

The following table lists the constants of the Int8Array object.

PropertyDescription
buffer PropertyRead-only. Gets the ArrayBuffer that is referenced by this array.
byteLength PropertyRead-only. The length of this array from the start of its ArrayBuffer, in bytes, as fixed at construction time.
byteOffset PropertyRead-only. The offset of this array from the start of its ArrayBuffer, in bytes, as fixed at construction time.
length PropertyThe length of the array.

Methods

The following table lists the methods of the Int8Array object.

MethodDescription
get MethodOmittable. Gets the element at the specified index.
set Method (Int8Array)Sets a value or an array of values.
subarray Method (Float64Array)Gets a new Int8Array view of the ArrayBuffer store for this array.

Attributions

  • Microsoft Developer Network: Article