1

TCP server is sending data into my Nodejs server. So I want to get buffer object data into array format in Nodejs.

Buffer data format: 
data = <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 ...>

I wanted to parse it into human-readable format. So I use data.toString() then it will return data in a human-readable JSON format as a string.

JSON.parse() function used to parse the string to JSON but it's showing JSON parsing error while converting String to JSON.

Can you provide me the correct code to fetch TCP data into array format in Nodejs?

Andrew
  • 373
  • 2
  • 15
Dilip Kheni
  • 485
  • 1
  • 3
  • 17
  • Maybe the answers from a [similar question](https://stackoverflow.com/questions/18148827/convert-buffer-to-array) will be helpful. – Ramiz Wachtler Nov 13 '19 at 12:59
  • `Array.from( buffer )` ? But since you need the JSON, rather find what is invalid about the JSON ? ( Paste the JSON string into jsonlint or similar and fix the invalid JSON syntax. ) – Shilly Nov 13 '19 at 13:00

1 Answers1

0
From Buffer to ArrayBuffer:

function toArrayBuffer(buf) {
    var ab = new ArrayBuffer(buf.length);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buf.length; ++i) {
        view[i] = buf[i];
    }
    return ab;
}