I'm writing an app in node.js, and see that I can do things like this:
var buf = new Buffer("Hello World!") console.log(buf.toString("hex")) console.log(buf.toString("utf8"))
And I know of 'ascii' as an encoding type (it'll take an ASCII code, such as 112
and turn it into a p
), but what other types of encoding can I do?
Buffers have a toString() method that you can use to convert the buffer to a string. By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer. from() , the toString() function gives you the original string back.
In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.
The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.
The official node.js documentation for Buffer
is the best place to check for something like this. As previously noted, Buffer
currently supports these encodings: 'ascii'
, 'utf8'
, 'utf16le'
/'ucs2'
, 'base64'
, 'base64url'
, 'latin1'
/'binary'
, and 'hex'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With