Convert buffer to array

convert buffer to string
node.js buffer
convert buffer to byte array node js
buffer.from base64
convert buffer to image nodejs
javascript buffer browser
buffer slice
typescript byte array

I am setting memcached with

$memcached->set("item" , ["1" => "hello"]);

anything work in PHP ,

In Node.js with memcached plugin , I get a buffer instead of array in result

<Buffer 61 3a 25 61 34 3a>

I can not convert such buffer to array

In Node.js :

memcached.get("item" , function(err, data) {
  console.log(data);
}

Do you have any way ?


arr = [...buffer]

ES6 introduced a lot of other features, besides buffers.

You can even easily append like this:

arr.push(...buffer)

The ... operator expands enumerables such as arrays and buffers when used in array. It also expands them into separate function arguments.


Yes, it's also faster:

... : x100000: 835.850ms

Slice call from prototype : x100000: 2118.513ms

var array,
    buffer = new Buffer([1, 4, 4, 5, 6, 7, 5, 3, 5, 67, 7, 4, 3, 5, 76, 234, 24, 235, 24, 4, 234, 234, 234, 325, 32, 6246, 8, 89, 689, 7687, 56, 54, 643, 32, 213, 2134, 235, 346, 45756, 857, 987, 0790, 89, 57, 5, 32, 423, 54, 6, 765, 65, 745, 4, 34, 543, 43, 3, 3, 3, 34, 3, 63, 63, 35, 7, 537, 35, 75, 754, 7, 23, 234, 43, 6, 247, 35, 54, 745, 767, 5, 3, 2, 2, 6, 7, 32, 3, 56, 346, 4, 32, 32, 3, 4, 45, 5, 34, 45, 43, 43]),
    iter = 100000;

array = buffer;

console.time("... : x" + iter);
for (var i = iter; i--;) array = [...buffer]
console.timeEnd("... : x" + iter);

console.time("Apply/call/etc : x" + iter);
for (var i = iter; i--;) array = Array.prototype.slice.call(buffer, 0)
console.timeEnd("Apply/call/etc : x" + iter);

Convert a binary NodeJS Buffer to JavaScript ArrayBuffer, js has both ArrayBuffer as part of v8, but the Buffer class provides a more flexible API. In order to read or write to an ArrayBuffer, you only need to create a view and � Node.js – Convert Array to Buffer Node.js – Convert Array to Buffer : To convert array (octet array/ number array/ binary array) to buffer, use Buffer.from(array) method. Syntax Buffer.from(array) Buffer.from method reads octets from array and returns a buffer initialized with those read bytes. Example – Read an octet array to buffer In the following example, an octet array is read to a


Here you go:

var buffer = new Buffer([1,2,3])
var arr = Array.prototype.slice.call(buffer, 0)
console.log(arr)

Buffer, When converting between Buffer s and strings, a character encoding may be Passing the Buffer s underlying ArrayBuffer will create a TypedArray that shares� Optionally we can provide byteOffset to start from (0 by default) and the length (till the end of the buffer by default), then the view will cover only a part of the buffer. If an Array, or any array-like object is given, it creates a typed array of the same length and copies the content. We can use it to pre-fill the array with the data:


There is another way to convert to array of integers

Using toJSON()

Buffer.from('Text of example').toJSON()
{ type: 'Buffer',data: [ 84, 101, 120, 116, 32, 111, 102, 32, 101, 120, 97, 109, 112, 108, 101 ] }

// simple get data
Buffer.from('Text of example').toJSON().data
[ 84, 101, 120, 116, 32, 111, 102, 32, 101, 120, 97, 109, 112, 108, 101 ]

Example of benchmark

// I took this from @user4584267's answer
const buffer = new Buffer([1, 4, 4, 5, 6, 7, 5, 3, 5, 67, 7, 4, 3, 5, 76, 234, 24, 235, 24, 4, 234, 234, 234, 325, 32, 6246, 8, 89, 689, 7687, 56, 54, 643, 32, 213, 2134, 235, 346, 45756, 857, 987, 0790, 89, 57, 5, 32, 423, 54, 6, 765, 65, 745, 4, 34, 543, 43, 3, 3, 3, 34, 3, 63, 63, 35, 7, 537, 35, 75, 754, 7, 23, 234, 43, 6, 247, 35, 54, 745, 767, 5, 3, 2, 2, 6, 7, 32, 3, 56, 346, 4, 32, 32, 3, 4, 45, 5, 34, 45, 43, 43]);
let array = null;
const iterations = 100000;

console.time("...buffer");
for (let i = iterations; i=i-1;) array = [...buffer]
console.timeEnd("...buffer");

console.time("array.prototype.slice.call");
for (let i = iterations; i=i-1;) array = Array.prototype.slice.call(buffer, 0)
console.timeEnd("array.prototype.slice.call");

console.time("toJSON().data");
for (let i = iterations; i=i-1;) array = buffer.toJSON().data
console.timeEnd("toJSON().data");

OUTPUT

...buffer: 559.932ms array.prototype.slice.call: 1176.535ms toJSON().data: 30.571ms

or if you want more profesional and custom function in Buffer use this:

Buffer.prototype.toArrayInteger = function(){
    if (this.length > 0) {
        const data = new Array(this.length);
        for (let i = 0; i < this.length; i=i+1)
            data[i] = this[i];
        return data;
    }
    return [];
}

Example of benchmark:

const buffer = new Buffer([1, 4, 4, 5, 6, 7, 5, 3, 5, 67, 7, 4, 3, 5, 76, 234, 24, 235, 24, 4, 234, 234, 234, 325, 32, 6246, 8, 89, 689, 7687, 56, 54, 643, 32, 213, 2134, 235, 346, 45756, 857, 987, 0790, 89, 57, 5, 32, 423, 54, 6, 765, 65, 745, 4, 34, 543, 43, 3, 3, 3, 34, 3, 63, 63, 35, 7, 537, 35, 75, 754, 7, 23, 234, 43, 6, 247, 35, 54, 745, 767, 5, 3, 2, 2, 6, 7, 32, 3, 56, 346, 4, 32, 32, 3, 4, 45, 5, 34, 45, 43, 43]);
let array = null;
const iterations = 100000;

console.time("toArrayInteger");
for (let i = iterations; i=i-1;) buffer.toArrayInteger();
console.timeEnd("toArrayInteger");

Ouput:

toArrayInteger: 28.714ms

Note: In the last example I copied a function from Buffer.toJSON and custom it a lite

ArrayBuffer, 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� 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".You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the


I haven't used memcached so I am not sure just what this buffer represents or what you want to have instead. Sorry. Here is a function to split a buffer up into an array of bytes. More at node.js Buffer docs, hope it helps!

var hex = new Buffer("613a2561343a", "hex");
var l = hex.length; // in bytes
var output = [];
for(var i = 0; i < l; i++){
  var char = hex.toString('hex',i,i+1); // i is byte index of hex
  output.push(char);
};
console.log(output);
// output: [ '61', '3a', '25', '61', '34', '3a' ]

Using Buffers in Node.js, A buffer acts like an array of integers, but cannot be resized. Converting between Buffers and JavaScript string objects requires an explicit� The following is a more complex example that creates a 20-element array of 32-bit integers. It then uses the BitConverter.GetBytes(Int32) method to convert each element into a byte array, which it stores in the appropriate position in a buffer by calling the Array.Copy(Array, Int32, Array, Int32, Int32) method.


You can also use Array.from:

memcached.get("item" , function(err, data) {
  console.log(Array.from(data));
}

Node.js - Convert Array to Buffer, Buffer.from method reads octets from array and returns a buffer initialized with those read bytes. Example – Read an octet array to buffer. In the following� 2) example - for cases where buffer is points to null terminated array of characters: std::string bufferToString(char* buffer) { return std::string(buffer); } 3) example - for cases where you pass char[]:


How to convert ArrayBuffer to and from String, How to convert ArrayBuffer to and from String. Since an ArrayBuffer is, in fact, a byte array, this conversion requires that both ends agree on� convert index to 6-bit number (binary string) join four 6 bit numbers which gives 24-bit numer (stored as binary string) split 24-bit string to three 8-bit and covert each to number and store them in output array; corner case: if input base64 string ends with one/two = char, remove one/two numbers from output array


Array Buffer -> String and String -> ArrayBuffer conversions in , Use this code to convert an array buffer to a number convertArrayBufferToNumber(buffer: ArrayBuffer){ const bytes = new Uint8Array( buffer); const dv = new� Then, to convert the array to a Buffer, we simply need to call the from class method, passing as input our array. const buffer = Buffer.from(testArray); After this we will print our buffer with a simple console log. console.log(buffer); You should get an output like figure 1. As can be seen, we have correctly obtained our Buffer from the array


Using Buffers in Node.js, Buffers store a sequence of integers, similar to an array in JavaScript. buffers, and using buffers to convert between binary and encoded data. The Encoding.GetBytes() method converts a string into a byte array in C#. This article includes a code example of how to convert a C# string into a byte array.