SOLFIND
Web Lens
Portal home

String decoder | Node.js v19.9.0 Documentation

https://nodejs.org/docs/latest-v19.x/api/string_decoder.html • 24 KB fetched
Open original page


String decoder | Node.js v19.9.0 Documentation Node.js * About this documentation * Usage and example * Assertion testing * Asynchronous context tracking * Async hooks * Buffer * C++ addons * C/C++ addons with Node-API * C++ embedder API * Child processes * Cluster * Command-line options * Console * Corepack * Crypto * Debugger * Deprecated APIs * Diagnostics Channel * DNS * Domain * Errors * Events * File system * Globals * HTTP * HTTP/2 * HTTPS * Inspector * Internationalization * Modules: CommonJS modules * Modules: ECMAScript modules * Modules: node:module API * Modules: Packages * Net * OS * Path * Performance hooks * Permissions * Process * Punycode * Query strings * Readline * REPL * Report * Single executable applications * Stream * String decoder * Test runner * Timers * TLS/SSL * Trace events * TTY * UDP/datagram * URL * Utilities * V8 * VM * WASI * Web Crypto API * Web Streams API * Worker threads * Zlib * Code repository and issue tracker Node.js v19.9.0 documentation * Node.js v19.9.0 * ► ▼ Table of contents * String decoder * Class: StringDecoder * new StringDecoder([encoding]) * stringDecoder.end([buffer]) * stringDecoder.write(buffer) * ► ▼ Index * About this documentation * Usage and example * Index * Assertion testing * Asynchronous context tracking * Async hooks * Buffer * C++ addons * C/C++ addons with Node-API * C++ embedder API * Child processes * Cluster * Command-line options * Console * Corepack * Crypto * Debugger * Deprecated APIs * Diagnostics Channel * DNS * Domain * Errors * Events * File system * Globals * HTTP * HTTP/2 * HTTPS * Inspector * Internationalization * Modules: CommonJS modules * Modules: ECMAScript modules * Modules: node:module API * Modules: Packages * Net * OS * Path * Performance hooks * Permissions * Process * Punycode * Query strings * Readline * REPL * Report * Single executable applications * Stream * String decoder * Test runner * Timers * TLS/SSL * Trace events * TTY * UDP/datagram * URL * Utilities * V8 * VM * WASI * Web Crypto API * Web Streams API * Worker threads * Zlib * Code repository and issue tracker * ► ▼ Other versions * 19.x * 18.x LTS * 17.x * 16.x LTS * 15.x * 14.x LTS * 13.x * 12.x * 11.x * 10.x * 9.x * 8.x * 7.x * 6.x * 5.x * 4.x * 0.12.x * 0.10.x * ► ▼ Options * View on single page * View as JSON * Edit on GitHub Table of contents * String decoder * Class: StringDecoder * new StringDecoder([encoding]) * stringDecoder.end([buffer]) * stringDecoder.write(buffer) String decoder # Stability: 2 - Stable Source Code: lib/string_decoder.js The node:string_decoder module provides an API for decoding Buffer objects into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16 characters. It can be accessed using: const { StringDecoder } = require ( 'node:string_decoder' ); copy The following example shows the basic use of the StringDecoder class. const { StringDecoder } = require ( 'node:string_decoder' ); 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)); copy When a Buffer instance is written to the StringDecoder 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 stringDecoder.write() or until stringDecoder.end() is called. In the following example, the three UTF-8 encoded bytes of the European Euro symbol ( € ) are written over three separate operations: const { StringDecoder } = require ( 'node:string_decoder' ); const decoder = new StringDecoder ( 'utf8' ); decoder. write ( Buffer . from ([ 0xE2 ])); decoder. write ( Buffer . from ([ 0x82 ])); console . log (decoder. end ( Buffer . from ([ 0xAC ]))); copy Class: StringDecoder # new StringDecoder([encoding]) # Added in: v0.1.99 * encoding <string> The character encoding the StringDecoder will use. Default: 'utf8' . Creates a new StringDecoder instance. stringDecoder.end([buffer]) # Added in: v0.9.3 * buffer <Buffer> | <TypedArray> | <DataView> A Buffer , or TypedArray , or DataView containing the bytes to decode. * Returns: <string> 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. If the buffer argument is provided, one final call to stringDecoder.write() is performed before returning the remaining input. After end() is called, the stringDecoder object can be reused for new input. stringDecoder.write(buffer) # History Version Changes v8.0.0 Each invalid character is now replaced by a single replacement character instead of one for each individual byte. v0.1.99 Added in: v0.1.99 * buffer <Buffer> | <TypedArray> | <DataView> A Buffer , or TypedArray , or DataView containing the bytes to decode. * Returns: <string> Returns a decoded string, ensuring that any incomplete multibyte characters at the end of the Buffer , or TypedArray , or DataView are omitted from the returned string and stored in an internal buffer for the next call to stringDecoder.write() or stringDecoder.end() .

Links found on this page

  1. Node.js [direct]
  2. About this documentation [direct]
  3. Usage and example [direct]
  4. Assertion testing [direct]
  5. Asynchronous context tracking [direct]
  6. Async hooks [direct]
  7. Buffer [direct]
  8. C++ addons [direct]
  9. C/C++ addons with Node-API [direct]
  10. C++ embedder API [direct]
  11. Child processes [direct]
  12. Cluster [direct]
  13. Command-line options [direct]
  14. Console [direct]
  15. Corepack [direct]
  16. Crypto [direct]
  17. Debugger [direct]
  18. Deprecated APIs [direct]
  19. Diagnostics Channel [direct]
  20. DNS [direct]
  21. Domain [direct]
  22. Errors [direct]
  23. Events [direct]
  24. File system [direct]
  25. Globals [direct]
  26. HTTP [direct]
  27. HTTP/2 [direct]
  28. HTTPS [direct]
  29. Inspector [direct]
  30. Internationalization [direct]
  31. Modules: CommonJS modules [direct]
  32. Modules: ECMAScript modules [direct]
  33. Modules: node:module API [direct]
  34. Modules: Packages [direct]
  35. Net [direct]
  36. OS [direct]
  37. Path [direct]
  38. Performance hooks [direct]
  39. Permissions [direct]
  40. Process [direct]
  41. Punycode [direct]
  42. Query strings [direct]
  43. Readline [direct]
  44. REPL [direct]
  45. Report [direct]
  46. Single executable applications [direct]
  47. Stream [direct]
  48. String decoder [direct]
  49. Test runner [direct]
  50. Timers [direct]
  51. TLS/SSL [direct]
  52. Trace events [direct]
  53. TTY [direct]
  54. UDP/datagram [direct]
  55. URL [direct]
  56. Utilities [direct]
  57. V8 [direct]
  58. VM [direct]
  59. WASI [direct]
  60. Web Crypto API [direct]
  61. Web Streams API [direct]
  62. Worker threads [direct]
  63. Zlib [direct]
  64. Code repository and issue tracker [direct]
  65. Index [direct]
  66. 18.x LTS [direct]
  67. 17.x [direct]
  68. 16.x LTS [direct]
  69. 15.x [direct]
  70. 14.x LTS [direct]
  71. 13.x [direct]
  72. 12.x [direct]
  73. 11.x [direct]
  74. 10.x [direct]
  75. 9.x [direct]
  76. 8.x [direct]
  77. 7.x [direct]
  78. 6.x [direct]
  79. 5.x [direct]
  80. 4.x [direct]