[ Web Proxy ]
URL:
Viewing: https://ja.javascript.info/arraybuffer-binary-arrays [Back]  [Original]

ArrayBuffer, binary arrays
:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
20211215

ArrayBuffer, binary arrays

Web ()

JavaScript

:

  • ArrayBuffer, Uint8Array, DataView, Blob, File, etc.

JavaScript

ArrayBuffer

:

let buffer = new ArrayBuffer(16); //  16 
alert(buffer.byteLength); // 16

16

ArrayBuffer

ArrayBuffer Array :

  • buffer[index] view

ArrayBuffer

ArrayBuffer (view)

ArrayBuffer

:

  • Uint8Array ArrayBuffer 0 255 (18) 8(8-bit unsigned integer)
  • Uint16Array 20 65535 16
  • Uint32Array 4 0 4294967295 32
  • Float64Array 8 5.0x10-324 1.8x10308

16 ArrayBuffer 16 , 8 (2), 4 (4), 2(8

[]

ArrayBuffer

e.g:

let buffer = new ArrayBuffer(16); //  16 

let view = new Uint32Array(buffer); // 32 

alert(Uint32Array.BYTES_PER_ELEMENT); // 4 

alert(view.length); // 4, 
alert(view.byteLength); // 16, 

// 
view[0] = 123456;

// 
for(let num of view) {
  alert(num); // 123456,  0, 0, 0 ( 4 )
}

TypedArray

(Uint8Array, Uint32Array, etc) TypedArray

: (iterable)

(Int8Array Float64Array )

5 :

new TypedArray(buffer, [byteOffset], [length]);
new TypedArray(object);
new TypedArray(typedArray);
new TypedArray(length);
new TypedArray();
  1. ArrayBuffer

    byteOffset ( 0)length () buffer

  2. Array

    :

    let arr = new Uint8Array([0, 1, 2, 3]);
    alert( arr.length ); // 4
    alert( arr[1] ); // 1
  3. TypedArray :

    let arr16 = new Uint16Array([1, 1000]);
    let arr8 = new Uint8Array(arr16);
    alert( arr8[0] ); // 1
    alert( arr8[1] ); // 232 (1000 1000  8 (1000  11 1110 1000, 232  1110 1000)
  4. length TypedArray.BYTES_PER_ELEMENT length :

    let arr = new Uint16Array(4); //  4 
    alert( Uint16Array.BYTES_PER_ELEMENT ); //  2 
    alert( arr.byteLength ); // 8 ()

ArrayBuffer TypedArray ArrayBuffer (ArrayBuffer )

ArrayBuffer :

  • arr.buffer ArrayBuffer
  • arr.byteLength ArrayBuffer

:

let arr8 = new Uint8Array([0, 1, 2, 3]);

// 
let arr16 = new Uint16Array(arr8.buffer);

:

  • Uint8Array, Uint16Array, Uint32Array 8, 16, 32
    • Uint8ClampedArray 8 ()
  • Int8Array, Int16Array, Int32Array (
  • Float32Array, Float64Array 32, 64
int8

Int8Array JavaScript int int8

Int8Array ArrayBuffer

256 Uint8Array ()256 100000000 (9)Uint8Array 8 0 255

()8:

[]

257 100000001 (9) 8 1 :

[]

:

let uint8array = new Uint8Array(16);

let num = 256;
alert(num.toString(2)); // 100000000 ()

uint8array[0] = 256;
uint8array[1] = 257;

alert(uint8array[0]); // 0
alert(uint8array[1]); // 1

Uint8ClampedArray 255 255 0

TypedArray

TypedArray Array

map, slice, find, reduce

:

  • splice buffer
  • concat

2:

  • arr.set(fromArr, [offset]) fromArr arr offset ( 0)
  • arr.subarray([begin, end]) begin end () slice ()

DataView

DataView ArrayBuffer

  • i arr[i]
  • DataView .getUint8(i) .getUint16(i)

:

new DataView(buffer, [byteOffset], [byteLength])
  • buffer ArrayBuffer DataView buffer
  • byteOffset ( 0)
  • byteLength ( buffer )

:

let buffer = new Uint8Array([255, 255, 255, 255]).buffer;

let dataView = new DataView(buffer);

//  0 8
alert( dataView.getUint8(0) ); // 255

//  0 16 2
alert( dataView.getUint16(0) ); // 65535 (16)

//  0 32
alert( dataView.getUint32(0) ); // 4294967295 (32)

dataView.setUint32(0, 0); // 4 0 

DataView E.g (1632 float)DataView

ArrayBuffer

ArrayBuffer

  • TypedArray:
    • Uint8Array, Uint16Array, Uint32Array 8, 16 32
    • Uint8ClampedArray 8
    • Int8Array, Int16Array, Int32Array ()
    • Float32Array, Float64Array 32, 64
  • DataView getUint8(offset).

ArrayBuffer .buffer

2:

  • ArrayBufferView
  • BufferSource ArrayBuffer ArrayBufferView

BufferSource ArrayBuffer

:

[]

Uint8Array concat(arrays)

function concat(arrays) {
  // sum of individual array lengths
  let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);

  if (!arrays.length) return null;

  let result = new Uint8Array(totalLength);

  // for each array - copy it over result
  // next array is copied right after the previous one
  let length = 0;
  for(let array of arrays) {
    result.set(array, length);
    length += array.length;
  }

  return result;
}


Web Proxy Viewer  |  New URL  |  Original Page