<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>String Decoder | Node.js v6.17.1 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/sh.css"> <link rel="canonical" href="https://nodejs.org/api/string_decoder.html"> </head> <body class="alt apidoc" id="api-section-string_decoder"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a class="nav-documentation" href="documentation.html">About these Docs</a></li> <li><a class="nav-synopsis" href="synopsis.html">Usage & Example</a></li> </ul> <div class="line"></div> <ul> <li><a class="nav-assert" href="assert.html">Assertion Testing</a></li> <li><a class="nav-buffer" href="buffer.html">Buffer</a></li> <li><a class="nav-addons" href="addons.html">C++ Addons</a></li> <li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li> <li><a class="nav-child_process" href="child_process.html">Child Processes</a></li> <li><a class="nav-cluster" href="cluster.html">Cluster</a></li> <li><a class="nav-cli" href="cli.html">Command Line Options</a></li> <li><a class="nav-console" href="console.html">Console</a></li> <li><a class="nav-crypto" href="crypto.html">Crypto</a></li> <li><a class="nav-debugger" href="debugger.html">Debugger</a></li> <li><a class="nav-dns" href="dns.html">DNS</a></li> <li><a class="nav-domain" href="domain.html">Domain</a></li> <li><a class="nav-errors" href="errors.html">Errors</a></li> <li><a class="nav-events" href="events.html">Events</a></li> <li><a class="nav-fs" href="fs.html">File System</a></li> <li><a class="nav-globals" href="globals.html">Globals</a></li> <li><a class="nav-http" href="http.html">HTTP</a></li> <li><a class="nav-https" href="https.html">HTTPS</a></li> <li><a class="nav-intl" href="intl.html">Internationalization</a></li> <li><a class="nav-modules" href="modules.html">Modules</a></li> <li><a class="nav-net" href="net.html">Net</a></li> <li><a class="nav-os" href="os.html">OS</a></li> <li><a class="nav-path" href="path.html">Path</a></li> <li><a class="nav-process" href="process.html">Process</a></li> <li><a class="nav-punycode" href="punycode.html">Punycode</a></li> <li><a class="nav-querystring" href="querystring.html">Query Strings</a></li> <li><a class="nav-readline" href="readline.html">Readline</a></li> <li><a class="nav-repl" href="repl.html">REPL</a></li> <li><a class="nav-stream" href="stream.html">Stream</a></li> <li><a class="nav-string_decoder active" href="string_decoder.html">String Decoder</a></li> <li><a class="nav-timers" href="timers.html">Timers</a></li> <li><a class="nav-tls" href="tls.html">TLS/SSL</a></li> <li><a class="nav-tty" href="tty.html">TTY</a></li> <li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li> <li><a class="nav-url" href="url.html">URL</a></li> <li><a class="nav-util" href="util.html">Utilities</a></li> <li><a class="nav-v8" href="v8.html">V8</a></li> <li><a class="nav-vm" href="vm.html">VM</a></li> <li><a class="nav-zlib" href="zlib.html">ZLIB</a></li> </ul> <div class="line"></div> <ul> <li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo & Issue Tracker</a></li> <li><a class="nav-http-groups-google-com-group-nodejs" href="http://groups.google.com/group/nodejs">Mailing List</a></li> </ul> </div> <div id="column1" data-id="string_decoder" class="interior"> <header> <h1>Node.js v6.17.1 Documentation</h1> <div id="gtoc"> <ul> <li> <a href="index.html" name="toc">Index</a> </li> <li> <a href="all.html">View on single page</a> </li> <li> <a href="string_decoder.json">View as JSON</a> </li> <li class="version-picker"> <a href="#">View another version <span>▼</span></a> <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/string_decoder.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/string_decoder.html">8.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/string_decoder.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/string_decoder.html">6.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/string_decoder.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/string_decoder.html">4.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/string_decoder.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/string_decoder.html">0.10.x</a></li></ol> </li> </ul> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li><span class="stability_2"><a href="#string_decoder_string_decoder">String Decoder</a></span><ul> <li><span class="stability_undefined"><a href="#string_decoder_class_new_stringdecoder_encoding">Class: new StringDecoder([encoding])</a></span><ul> <li><span class="stability_undefined"><a href="#string_decoder_stringdecoder_end_buffer">stringDecoder.end([buffer])</a></span></li> <li><span class="stability_undefined"><a href="#string_decoder_stringdecoder_write_buffer">stringDecoder.write(buffer)</a></span></li> </ul> </li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>String Decoder<span><a class="mark" href="#string_decoder_string_decoder"id="string_decoder_string_decoder">#</a></span></h1> <!--introduced_in=v0.10.0--> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>The <code>string_decoder</code> module provides an API for decoding <code>Buffer</code> objects into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16 characters. It can be accessed using:</p> <pre><code class="lang-js">const StringDecoder = require('string_decoder').StringDecoder; </code></pre> <p>The following example shows the basic use of the <code>StringDecoder</code> class.</p> <pre><code class="lang-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)); </code></pre> <p>When a <code>Buffer</code> instance is written to the <code>StringDecoder</code> instance, an internal buffer is used to ensure that the decoded string does not contain any incomplete multibyte characters. These are held in the buffer until the next call to <code>stringDecoder.write()</code> or until <code>stringDecoder.end()</code> is called.</p> <p>In the following example, the three UTF-8 encoded bytes of the European Euro symbol (<code>€</code>) are written over three separate operations:</p> <pre><code class="lang-js">const StringDecoder = require('string_decoder').StringDecoder; const decoder = new StringDecoder('utf8'); decoder.write(Buffer.from([0xE2])); decoder.write(Buffer.from([0x82])); console.log(decoder.end(Buffer.from([0xAC]))); </code></pre> <h2>Class: new StringDecoder([encoding])<span><a class="mark" href="#string_decoder_class_new_stringdecoder_encoding"id="string_decoder_class_new_stringdecoder_encoding">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.99</span> </div><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The character encoding the <code>StringDecoder</code> will use. Defaults to <code>'utf8'</code>.</li> </ul> <p>Creates a new <code>StringDecoder</code> instance.</p> <h3>stringDecoder.end([buffer])<span><a class="mark" href="#string_decoder_stringdecoder_end_buffer"id="string_decoder_stringdecoder_end_buffer">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.9.3</span> </div><ul> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A <code>Buffer</code> containing the bytes to decode.</li> </ul> <p>Returns any remaining input stored in the internal buffer as a string. Bytes representing incomplete UTF-8 and UTF-16 characters will be replaced with substitution characters appropriate for the character encoding.</p> <p>If the <code>buffer</code> argument is provided, one final call to <code>stringDecoder.write()</code> is performed before returning the remaining input.</p> <h3>stringDecoder.write(buffer)<span><a class="mark" href="#string_decoder_stringdecoder_write_buffer"id="string_decoder_stringdecoder_write_buffer">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.99</span> </div><ul> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A <code>Buffer</code> containing the bytes to decode.</li> </ul> <p>Returns a decoded string, ensuring that any incomplete multibyte characters at the end of the <code>Buffer</code> are omitted from the returned string and stored in an internal buffer for the next call to <code>stringDecoder.write()</code> or <code>stringDecoder.end()</code>.</p> </div> </div> </div> <script src="assets/sh_main.js"></script> <script src="assets/sh_javascript.min.js"></script> <script>highlight(undefined, undefined, 'pre');</script> <!-- __TRACKING__ --> </body> </html>