Use string_decoder module in Nodejs to convert buffer into string

青灯夜游
Release: 2021-05-14 11:00:36
forward
2601 people have browsed it

This article will introduce to you how to use the string_decoder module to convert buffer into string inNodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Use string_decoder module in Nodejs to convert buffer into string

Module Introduction

string_decoderThe module is used to convert Buffer into the corresponding string. Users can obtain the string corresponding to the buffer by callingstringDecoder.write(buffer). [Recommended learning: "nodejs Tutorial"]

What's special about it is that when the incoming buffer is incomplete (for example, three bytes of characters, only two are passed in) ), an internal buffer will be maintained internally to cache the incomplete bytes, and wait until the user callsstringDecoder.write(buffer)again to pass in the remaining bytes to spell out complete characters.

This can effectively avoid errors caused by incomplete buffer, and is very useful for many scenarios, such as package body parsing in network requests, etc.

Getting Started Example

This section demonstratesdecode.write(buffer),decode.end([buffer])# respectively. ##Usage of two main APIs.

Example 1:

decoder.write(buffer)The call passes in the Buffer object , and returns accordingly The corresponding stringyou ;

const StringDecoder = require('string_decoder').StringDecoder; const decoder = new StringDecoder('utf8'); // Buffer.from('你') =>  const str = decoder.write(Buffer.from([0xe4, 0xbd, 0xa0])); console.log(str); // 你
Copy after login

Example 2:

When

decoder.end([buffer])is called, the internal remaining The buffer will be returned at once. If you bring thebufferparameter at this time, it is equivalent to callingdecoder.write(buffer)anddecoder.end()at the same time.

const StringDecoder = require('string_decoder').StringDecoder; const decoder = new StringDecoder('utf8'); // Buffer.from('你好') =>  let str = decoder.write(Buffer.from([0xe4, 0xbd, 0xa0, 0xe5, 0xa5])); console.log(str); // 你 str = decoder.end(Buffer.from([0xbd])); console.log(str); // 好
Copy after login

Example: Write multiple bytes multiple times

The following example demonstrates how to write multiple bytes multiple times

How the string_decodermodule handles it.

First,

is passed in,Okay is still 1 byte short, at this time,decoder.write (xx) Returnyou .

Then, call

decoder.write(Buffer.from([0xbd]))again, pass in the remaining 1 byte, and successfully returnGood.

const StringDecoder = require('string_decoder').StringDecoder; const decoder = new StringDecoder('utf8'); // Buffer.from('你好') =>  let str = decoder.write(Buffer.from([0xe4, 0xbd, 0xa0, 0xe5, 0xa5])); console.log(str); // 你 str = decoder.write(Buffer.from([0xbd])); console.log(str); // 好
Copy after login

Example: When decoder.end(), the number of bytes is incomplete The first byte ofis passed in. At this time,

decoder.end()

is called, andis returned, and the corresponding buffer is< ;Buffer ef bf bd>.

const StringDecoder = require(&#39;string_decoder&#39;).StringDecoder; // Buffer.from(&#39;好&#39;) => <Buffer e5 a5 bd> let decoder = new StringDecoder(&#39;utf8&#39;); let str = decoder.end( Buffer.from([0xe5]) ); console.log(str); // � console.log(Buffer.from(str)); // <Buffer ef bf bd>
Copy after login
The official document explains this situation like this (almost like nonsense). It is roughly a convention. When the utf8code point is invalid, replace it withef bf bd.

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.

Related links

A UTF-8 character you should remember "EF BF BD" http://liudanking.com/golang/utf-8_replacement_character/

For more programming-related knowledge, please visit:Programming Video! !

The above is the detailed content of Use string_decoder module in Nodejs to convert buffer into string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!