Node.jsAPI中怎么使用string_decoder-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了Node.js API中怎么使用string_decoder,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

創(chuàng)新互聯(lián)專注于中大型企業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)和網(wǎng)站改版、網(wǎng)站營(yíng)銷服務(wù),追求商業(yè)策劃與數(shù)據(jù)分析、創(chuàng)意藝術(shù)與技術(shù)開發(fā)的融合,累計(jì)客戶千余家,服務(wù)滿意度達(dá)97%。幫助廣大客戶順利對(duì)接上互聯(lián)網(wǎng)浪潮,準(zhǔn)確優(yōu)選出符合自己需要的互聯(lián)網(wǎng)運(yùn)用,我們將一直專注成都品牌網(wǎng)站建設(shè)和互聯(lián)網(wǎng)程序開發(fā),在前進(jìn)的路上,與客戶一起成長(zhǎng)!

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

對(duì)于參數(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?

分享文章:Node.jsAPI中怎么使用string_decoder-創(chuàng)新互聯(lián)
路徑分享:http://www.muchs.cn/article32/djhgsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司建站公司、軟件開發(fā)網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)