Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Uint8Array[n] into integer in node.js

I have a variable Uint8Arr of type Uint8Array[4].

Uint8Arr[0]=0x12;
Uint8Arr[1]=0x19;
Uint8Arr[2]=0x21;
Uint8Arr[3]=0x47;

I want to convert Uint8Arr into its equivalent integer which is 0x12192147 or 303636807.

I would like to have a function that can convert Uint8Arr[n] into its equivalent integer and return the result in decimal.


1 Answers

For those who want little-endian, you can specify endianness using the DataView class.

let buff = new Uint8Array(4);
buff[0]=0x12;
buff[1]=0x19;
buff[2]=0x21;
buff[3]=0x47;
var view = new DataView(buff.buffer, 0);
view.getUint32(0, true); // true here represents little-endian
like image 121
Eli Davis Avatar answered Jul 18 '26 14:07

Eli Davis