欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js API詳解之 string_decoder用法實(shí)例分析

 更新時(shí)間:2020年04月29日 08:46:58   作者:李小強(qiáng)  
這篇文章主要介紹了Node.js API詳解之 string_decoder用法,結(jié)合實(shí)例形式分析了Node.js API中string_decoder的功能、用法及操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Node.js API詳解之 string_decoder用法。分享給大家供大家參考,具體如下:

string_decoder 模塊提供了一個(gè) API,用于把 Buffer 對象解碼成字符串。

對于參數(shù)末尾不完整的多字節(jié)字符,string_decoder會(huì)將其保存在內(nèi)部的buffer中,當(dāng)再次解碼時(shí),補(bǔ)充到參數(shù)開頭。

通過 const { StringDecoder } = require(‘string_decoder'); 的方式引用string_decoder模塊。

目錄:

  • new StringDecoder([encoding])
  • stringDecoder.write(buffer)
  • stringDecoder.end([buffer])

new StringDecoder([encoding])

說明:

創(chuàng)建一個(gè)新的StringDecoder實(shí)例,可傳遞encoding參數(shù)作為字符編碼格式,默認(rèn)為'utf8′

stringDecoder.write(buffer)

說明:

返回一個(gè)解碼后的字符串,并確保返回的字符串不包含殘缺的多字節(jié)字符,殘缺的多字節(jié)字符會(huì)被保存在一個(gè)內(nèi)部的 buffer 中,
用于下次調(diào)用 stringDecoder.write() 或 stringDecoder.end()。
buffer:待解碼的Buffer

demo:

const decoder = new StringDecoder('utf8');
 
//字符的16進(jìn)制小于0x80屬于單字節(jié)
let outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoqiang
 
//字符的16進(jìn)制大于0x80屬于雙字節(jié)
outString = decoder.write(Buffer.from([0xC2, 0xA2]));
 
console.log(outString);
//¢
 
//單雙字節(jié)混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.write(Buffer.from([0xA2]));
 
console.log(outString);
//¢
 
//單雙字節(jié)混合,置于中間
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71,       0xC2, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoq?iang
 
outString = decoder.write(Buffer.from([0xA2]));
 
console.log(outString);
//?
 
//單雙字節(jié)混合,置于開始
outString = decoder.write(Buffer.from([0xC2,     0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//?xiaoqiang
 
outString = decoder.write(Buffer.from([0xA2]));
 
console.log(outString);
//?
 
//單雙字節(jié)混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.write(Buffer.from([0x78,0xA2]));
 
console.log(outString);
//?x?

stringDecoder.end([buffer])

說明:

以字符串的形式返回內(nèi)部 buffer 中剩余的字節(jié),殘缺的字節(jié)會(huì)被替換成符合字符編碼的字符
如果提供了 buffer 參數(shù),則在返回剩余字節(jié)之前會(huì)再執(zhí)行一次 stringDecoder.write()

demo:

const decoder = new StringDecoder('utf8');
 
//字符的16進(jìn)制小于0x80屬于單字節(jié)
let outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.end();
 
console.log(outString);
//
 
//單雙字節(jié)混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.end(Buffer.from([0xA2]));
 
console.log(outString);
//¢
 
//單雙字節(jié)混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.end();
 
console.log(outString);
//?

希望本文所述對大家node.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • npm全局模塊卸載及默認(rèn)安裝目錄修改方法

    npm全局模塊卸載及默認(rèn)安裝目錄修改方法

    今天小編就為大家分享一篇npm全局模塊卸載及默認(rèn)安裝目錄修改方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • nodejs爬蟲抓取數(shù)據(jù)亂碼問題總結(jié)

    nodejs爬蟲抓取數(shù)據(jù)亂碼問題總結(jié)

    這篇文章主要給大家總結(jié)了下nodejs爬蟲抓取數(shù)據(jù)亂碼問題的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • Nodejs libuv運(yùn)行原理詳解

    Nodejs libuv運(yùn)行原理詳解

    在本篇文章里小編給大家整理的是關(guān)于Nodejs libuv運(yùn)行原理以及相關(guān)知識點(diǎn),有需要的朋友們可以學(xué)習(xí)下。
    2019-08-08
  • 解析NodeJS異步I/O的實(shí)現(xiàn)

    解析NodeJS異步I/O的實(shí)現(xiàn)

    本篇文章主要介紹了解析NodeJS異步I/O的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 詳解nodejs模板引擎制作

    詳解nodejs模板引擎制作

    本篇文章主要介紹了nodejs模板引擎制作 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 如何在node.js中使用?JsonWebToken模塊進(jìn)行token加密

    如何在node.js中使用?JsonWebToken模塊進(jìn)行token加密

    目前在web框架中最流行的身份驗(yàn)證是使用jsonwebtoken,簡稱jwt.可以設(shè)置加密方式,過期時(shí)間,存放個(gè)人信息,逆解析,下面這篇文章主要給大家介紹了關(guān)于如何在node.js中使用?JsonWebToken模塊進(jìn)行token加密的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • 解決npm?i?報(bào)錯(cuò)以及python安裝卡住的問題

    解決npm?i?報(bào)錯(cuò)以及python安裝卡住的問題

    這篇文章主要介紹了解決npm?i?報(bào)錯(cuò)以及python安裝卡住的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Node.js發(fā)出請求走Proxyman代理調(diào)試tip詳解

    Node.js發(fā)出請求走Proxyman代理調(diào)試tip詳解

    這篇文章主要為大家介紹了Node.js發(fā)出請求走Proxyman代理調(diào)試tip詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • nodejs對文件中的圖片進(jìn)行歸類操作示例

    nodejs對文件中的圖片進(jìn)行歸類操作示例

    這篇文章主要為大家介紹了nodejs對文件中的圖片進(jìn)行歸類的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 完美解決node.js中使用https請求報(bào)CERT_UNTRUSTED的問題

    完美解決node.js中使用https請求報(bào)CERT_UNTRUSTED的問題

    下面小編就為大家?guī)硪黄昝澜鉀Qnode.js中使用https請求報(bào)CERT_UNTRUSTED的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論