节点。js API中怎么使用string_decoder

  介绍

这篇文章主要为大家展示了节点。js API中怎么使用string_decoder,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

string_decoder模块提供了一个API,用于把缓冲区对象解码成字符串。

对于参数末尾不完整的多字节字符,string_decoder会将其保存在内部的缓冲区中,当再次解码时,补充到参数开头。

通过const {StringDecoder}=要求(“string_decoder& # 39;);的方式引用string_decoder模块。

目录:

    <李>新StringDecoder([编码]) <李> stringDecoder.write(缓冲) <李> stringDecoder.end((缓冲))
      李,

说明:

创建一个新的StringDecoder实例,可传递编码参数作为字符编码格式,默认为& # 39;use utf8′

说明:

返回一个解码后的字符串,并确保返回的字符串不包含残缺的多字节字符,残缺的多字节字符会被保存在一个内部的缓冲区中,
用于下次调用stringDecoder.write()或stringDecoder.end ()。
缓冲:待解码的缓冲

演示:

 const解码器=new StringDecoder (& # 39; use utf8 # 39;);//字符的16进制小于0 x80属于单字节
  让outString=decoder.write (Buffer.from ([0 x78, 0 x69 0 x61 0 x6f, 0 x71 0 x69 0 x61 0 x6e 0 x67]));
  
  console.log (outString);//小强//字符的16进制大于0 x80属于双字节
  outString=decoder.write (Buffer.from ([0 xc2, 0 xa2]));
  
  console.log (outString);//?/单双字节混合,置于末尾
  x78 outString=decoder.write (Buffer.from ([0, 0 x69 0 x61 0 x6f 0 x71 0 x69 0 x61 0 x6e 0 x67 0 xc2]));
  
  console.log (outString);//小强
  
  outString=decoder.write (Buffer.from ([0 xa2]));
  
  console.log (outString);//?/单双字节混合,置于中间
  x78 outString=decoder.write (Buffer.from ([0, 0 x69 0 x61 0 x6f 0 x71 0 xc2 0 x69 0 x61 0 x6e 0 x67]));
  
  console.log (outString);//xiaoq& # 63; iang
  
  outString=decoder.write (Buffer.from ([0 xa2]));
  
  console.log (outString);//和# 63;//单双字节混合,置于开始
  xc2 outString=decoder.write (Buffer.from ([0, 0 x78 0 x69 0 x61 0 x6f 0 x71 0 x69 0 x61 0 x6e 0 x67]));
  
  console.log (outString);//和# 63;小强
  
  outString=decoder.write (Buffer.from ([0 xa2]));
  
  console.log (outString);//和# 63;//单双字节混合,置于末尾
  x78 outString=decoder.write (Buffer.from ([0, 0 x69 0 x61 0 x6f 0 x71 0 x69 0 x61 0 x6e 0 x67 0 xc2]));
  
  console.log (outString);//小强
  
  outString=decoder.write (Buffer.from ([0 x78, 0 xa2]));
  
  console.log (outString);//和# 63;x& # 63; 

说明:

以字符串的形式返回内部缓冲区中剩余的字节,残缺的字节会被替换成符合字符编码的字符
如果提供了缓冲参数,则在返回剩余字节之前会再执行一次stringDecoder.write ()

演示:

 const解码器=new StringDecoder (& # 39; use utf8 # 39;);//字符的16进制小于0 x80属于单字节
  让outString=decoder.write (Buffer.from ([0 x78, 0 x69 0 x61 0 x6f, 0 x71 0 x69 0 x61 0 x6e 0 x67]));
  
  console.log (outString);//小强
  
  outString=decoder.end ();
  
  console.log (outString);////单双字节混合,置于末尾
  x78 outString=decoder.write (Buffer.from ([0, 0 x69 0 x61 0 x6f 0 x71 0 x69 0 x61 0 x6e 0 x67 0 xc2]));
  
  console.log (outString);//小强
  
  outString=decoder.end (Buffer.from ([0 xa2]));
  
  console.log (outString);//?/单双字节混合,置于末尾
  x78 outString=decoder.write (Buffer.from ([0, 0 x69 0 x61 0 x6f 0 x71 0 x69 0 x61 0 x6e 0 x67 0 xc2]));
  
  console.log (outString);//小强
  
  outString=decoder.end ();
  
  console.log (outString); 

以上就是关于节点。js API中怎么使用string_decoder的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看的到。

节点。js API中怎么使用string_decoder