2

How to convert a buffer in an array?

Buffer (Hex values):

myBuffer = <Buffer 48 65 6c 6c 6F >

I want a a variable type array like this:

myArray = [48, 65, 6c, 6c, 6F] // Hex values
  or  
myArray = [72, 101, 108, 108, 111] // Decimal values

IMPORTANT: I don't want to use a for loop to traverse byte to byte of the buffer. I want to know if there is a native way to convert Buffer to Array.

8
  • <Buffer 48 65 6c 6c 6F > is string or tag.And what is the relation ship between [48, 65, 6c, 6c, 6F] or like this [72, 101, 108, 108, 111] these arrays ? .In html <Buffer 48 65 6c 6c 6F > is invalid one Commented Mar 21, 2019 at 2:57
  • @prasanth the first array appears to be in hexadecimal and the second array is the hexadecimal values in decimal it seems. I think they are referring to Buffer objects in Node, not strings/tags. Commented Mar 21, 2019 at 3:01
  • @prasanth OP is talking about ArrayBuffer: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 21, 2019 at 3:02
  • 2
    @prasanth That's probably because OP doesn't know how to write it, or maybe I'm indeed wrong and this is a completely different thing... anyway, the best course of action here is voting to close as "unclear what you're asking". Commented Mar 21, 2019 at 3:08
  • 2
    @wBB As Buffer is not an actual thing in native JavaScript. You could be either referring to ArrayBuffer or Node.js Buffer. Though the format <Buffer 48 65 6c 6c 6f> looks like Node.js Buffer in console.log to me so I guess you are referring to Node.js Buffer. Commented Mar 21, 2019 at 3:16

3 Answers 3

1

Looks like you are referring to Node.js Buffer. If this is the case, you can simply do this:

var buf = new Buffer([72,101,108,108,111]) //<Buffer 48 65 6c 6c 6f>
var arr = Array.prototype.slice.call(buf) //[72,101,108,108,111]
Sign up to request clarification or add additional context in comments.

Comments

1

In ES6, this would also work:

const buffer = new Buffer([72,101,108,108,111])
const arr = [...buffer] //[72,101,108,108,111]

Comments

0

If you're talking about arraybuffer, try this method:

function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}

If you're talking about nodejs buffer object, try this:

let bufferOne = Buffer.from('This is a buffer example.');
console.log(bufferOne);

// Output: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>

let json = JSON.stringify(bufferOne);
console.log(json);

// Output: {"type":"Buffer","data":   [84,104,105,115,32,105,115,32,97,32,98,117,102,102,101,114,32,101,120,97,109,112,108,101,46]}
json = JSON.parse(json);
json.data.forEach(e=> console.log(e.toString(16))); 
// Output: 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.