2

I am truly struggling to convert a Buffer to a string.

For this code

let results = await generateRandomNumber(seed);
console.log(results); 
    res.status(200).json({
    "status": 200,
    "number": results
});

}

I receive for the console.log(results);

   {
  result: <Buffer ee 62 e7 6f c7 4b 7f 57 86 20 bd ba 52 74 4a fc 66 89 70 bb>
}

And the response JSON is:

{"status":200,"number":{"result":{"type":"Buffer","data":[238,98,231,111,199,75,127,87,134,32,189,186,82,116,74,252,102,137,112,187]}}}

All I want to achieve is that the number actually becomes a number and not the Buffer.

3 Answers3

1

There are node buffer.read < primitive type > functions in documentation ready to use.

Lets take for example buf.readBigInt64BE([offset])

const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log(buf.readBigUInt64BE(0));

Your buffer has 20 hex values and if you want convert that into a number im not sure there will be a type to hold such thing.

Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
1

Have you tried this?

results.toString('utf8')

So you can access your json.data f

Andre Sampaio
  • 372
  • 3
  • 7
0

You may try like this

let results = await generateRandomNumber(seed);
var bufferData = Buffer.from(results);
var resultString = bufferData.toString('utf8');

console.log(results); 
    res.status(200).json({
    "status": 200,
    "number": resultString
});
Afeesudheen
  • 936
  • 2
  • 12
  • 21