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 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried this?

results.toString('utf8')

So you can access your json.data f

1 Comment

This simply returns [object Object]
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
});

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.