Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What encodings does Buffer.toString() support?

Tags:

node.js

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?

like image 362
Grayda Avatar asked Dec 27 '15 01:12

Grayda


People also ask

How do you convert a buffer to a string value?

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.

How do I decode a buffer in Node JS?

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.

What is the use of buffer in Nodejs?

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.


1 Answers

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'.

like image 189
mscdex Avatar answered Sep 18 '22 14:09

mscdex