5

I'm trying to figure out what encodings are available in node.js.

The documentation (http://nodejs.org/api/buffer.html#buffer_new_buffer_str_encoding) says:

Allocates a new buffer containing the given str. encoding defaults to 'utf8'.

but nowhere is specified a list of available encodings. Maybe I missed it.

I'm working on script which should be able to output in wide range of encodings. So far I know only about utf8 as doc is saying :)

Thx, Jaro.

Jaro
  • 3,799
  • 5
  • 31
  • 47
  • 2
    Check out [`iconv-lite`](https://npmjs.org/package/iconv-lite) if you need more encodings than supported natively by Node. – robertklep Nov 26 '13 at 18:19
  • Have to admit, I came here in exactly the same circumstance. The nodejs documentation is still poorly structured and not including any links/references to supported encodings for read and write operations is bad. – adelphus Jul 29 '15 at 10:21

2 Answers2

6

Encodings available in Node.js:

  • ascii: For 7-bit ASCII data only. This encoding is fast and will strip the high bit if set.
  • utf8: Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.
  • utf16le: 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.
  • ucs2: Alias of utf16le.
  • base64: Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC 4648, Section 5.
  • latin1: A way of encoding the Buffer into a one-byte encoded string (as defined by the IANA in RFC 1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes).
  • binary: Alias for 'latin1'.
  • hex: Encode each byte as two hexadecimal characters.

Source: Node 12 Buffer documentation

bnp887
  • 5,128
  • 2
  • 26
  • 30
dee-see
  • 23,668
  • 5
  • 58
  • 91
  • 1
    by default buffer does not support iso-8859-1. I googled around and found another thread http://stackoverflow.com/questions/14551608/cant-find-encodings-for-node-js . The solution is based on node-iconv npm. I'll give it a try. Thanks. – Jaro Nov 26 '13 at 18:22
0

in order to clarify lastest nodejs encoding explaination. I paste from nodejs document. v4+ does't deprated binary

The character encodings currently supported by Node.js include:

'ascii' - For 7-bit ASCII data only. This encoding is fast and will strip the high bit if set.

'utf8' - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'utf16le' - 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.

'ucs2' - Alias of 'utf16le'.

'base64' - Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC 4648, Section 5.

'latin1' - A way of encoding the Buffer into a one-byte encoded string (as defined by the IANA in RFC 1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes).

'binary' - Alias for 'latin1'.

'hex' - Encode each byte as two hexadecimal characters.

Community
  • 1
  • 1
xsilen T
  • 1,515
  • 14
  • 10