Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uint16Array to Uint8Array

I have a basic question. Say I have a Uint16Array and I have number 4 in it.

data_16=new Uint16Array([4]);  

Now I have a length 1 and byteLength 2;

how do i convert this to Uint8Array.

I do not want to create a new view.

data_8 = new Uint8Array(data_16)

If I do this I get array length 1 and byteLength 1. This is not what I want.

I need to stretch that 16 bit value in 16array into 8 bit values so that 8bit array so it would end up with 2 values int 8 bit array.

I can just create a funtion which converts with shif and to all that stuff. But Can it be done with Array manipulation only?

like image 474
Evren Bingøl Avatar asked Nov 20 '14 11:11

Evren Bingøl


People also ask

Is Uint8Array same as ArrayBuffer?

Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a byte is 8-bit, so it can hold only that much). Such value is called a “8-bit unsigned integer”. Uint16Array – treats every 2 bytes as an integer, with possible values from 0 to 65535.

What is a Uint8Array?

The Uint8Array typed array represents an array of 8-bit unsigned integers. The contents are initialized to 0 . Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).

What is Uint16Array?

The Uint16Array typed array represents an array of 16-bit unsigned integers in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0 .

Is ArrayBuffer same as buffer?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".


1 Answers

You can use the buffer property, i.e. data_8 = new Uint8Array(data_16.buffer, data_16.byteOffset, data_16.byteLength).

like image 85
Steve Campbell Avatar answered Sep 30 '22 15:09

Steve Campbell