0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 15:06:33 +01:00
nodejs/doc/api/string_decoder.markdown

29 lines
738 B
Markdown
Raw Normal View History

2012-04-20 08:32:58 +02:00
# StringDecoder
Stability: 2 - Stable
2012-04-20 08:32:58 +02:00
To use this module, do `require('string_decoder')`. StringDecoder decodes a
buffer to a string. It is a simple interface to `buffer.toString()` but provides
additional support for utf8.
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
2012-04-20 08:32:58 +02:00
const cent = new Buffer([0xC2, 0xA2]);
2012-04-20 08:32:58 +02:00
console.log(decoder.write(cent));
const euro = new Buffer([0xE2, 0x82, 0xAC]);
2012-04-20 08:32:58 +02:00
console.log(decoder.write(euro));
## Class: StringDecoder
Accepts a single argument, `encoding` which defaults to `'utf8'`.
2012-04-20 08:32:58 +02:00
### decoder.end()
Returns any trailing bytes that were left in the buffer.
### decoder.write(buffer)
Returns a decoded string.