English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Node.js – Convertire array in bufferPer convertire un array (array di byte a otto bit, array numerico, array binario) in un buffer, utilizzare Buffer.from(array)
Metodo.
Buffer. |
Il metodo Buffer.from legge array di byte e restituisce un buffer inizializzato con questi byte letti.
Nell'esempio seguente, l'array di byte a otto bit viene letto nel buffer.
var arr = [0x74, 0x32, 0x91]; const buf = Buffer.from(arr); for(const byt of buf.values()){ console.log(byt); }
Risultato di output
$ node array-to-buffer.js 116 50 145
Abbiamo registrato i dati di ogni byte come numeri.
0x74 = 0111 0100 = 116 0x32 = 0011 0010 = 50 0x91 = 1001 0001 = 145
Nell'esempio seguente, l'array numerico viene letto nel buffer.
var arr = [74, 32, 91]; const buf = Buffer.from(arr); for(const byt of buf.values()){ console.log(byt); }
Risultato di output
$ node array-to-buffer.js 74 32 91
Abbiamo registrato i dati di ogni byte come numeri.
Nell'esempio seguente, l'array di byte a otto bit viene letto nel buffer.
var arr = [true, true, false]; const buf = Buffer.from(arr); for(const byt of buf.values()){ console.log(byt); }
Risultato di output
$ node array-to-buffer.js 1 1 0
true è 1, false è 0.
In questo tutorial di Node.js – Node.js converte array in bufferabbiamo imparato come convertire array di byte a otto bit, array numerici e array booleani in buffer di Node.js.