summaryrefslogtreecommitdiff
path: root/doc/api/string_decoder.md
blob: 06b97bab489a85c35e1615b1352908d5f3353b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# StringDecoder

    Stability: 2 - Stable

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.

```js
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');

const cent = Buffer.from([0xC2, 0xA2]);
console.log(decoder.write(cent));

const euro = Buffer.from([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro));
```

## Class: StringDecoder
<!-- YAML
added: v0.1.99
-->

Accepts a single argument, `encoding` which defaults to `'utf8'`.

### decoder.end()
<!-- YAML
added: v0.9.3
-->

Returns any trailing bytes that were left in the buffer.

### decoder.write(buffer)
<!-- YAML
added: v0.1.99
-->

Returns a decoded string.