0

I want to convert a string that represents a Buffer into a string that is what the Buffer was encoding. For example, if I have the string

var str1 = "hello,there" 

And then I convert it to a buffer using Buffer.from()

buf1 = Buffer.from(str1)
<Buffer 68 65 6c 6c 6f 2c 74 68 65 72 65> 

Then if I take the String

str2 = "68656c6c6f2c7468657265"

And convert it back to give me EITHER the buffer again:

<Buffer 68 65 6c 6c 6f 2c 74 68 65 72 65>

OR simply just (since you can convert the buffer back to the string with .toString())

"hello,there" 

1 Answer 1

1

Buffer's toString function accepts an encoding type. In this case, use "hex":

buf1.toString( "hello,there", "hex" ) // "68656c6c6f2c7468657265"

The Buffer.from function also accepts an encoding type as its second parameter:

Buffer.from( "68656c6c6f2c7468657265", "hex") // "hello,there"
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.