1

I am trying to convert a string to buffer using nodejs. The string is here :

\xf5\x89\xf8\x19+q@o\xa7\xdcwa2\x8f@\x0c\xd1\x14\x8bT6u@~a]HG\x83

When I am doing this from console, I get a simple buffer. But if I read that from a text file, the corresponding buffer results in a different way. I am working with the following code :

var data = '\xf5\x89\xf8\x19+q@o\xa7\xdcwa2\x8f@\x0c\xd1\x14\x8bT6u@~a]HG\x83';
const buf = Buffer.from(data, 'ascii');    // Converting string to buffer
console.log(buf);
// While reading from file
fs = require('fs')
fs.readFile('notes.txt', function (err,data) {
   console.log(data);
});

Is it possible to get the same buffer in both way?

1 Answer 1

2

In the data variable you declare this value "\xf5\x89\xf8\x19+q@o\xa7\xdcwa2\x8f@\x0c\xd1\x14\x8bT6u@~a]HG\x83"

where "\" character does not count when you convert it to Buffer.

Please use double back slash. See example:

var data = '\\xf5\\x89\\xf8\\x19+q@o\\xa7\\xdcwa2\\x8f@\\x0c\\xd1\\x14\\x8bT6u@~a]HG\\x83';
Sign up to request clarification or add additional context in comments.

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.