最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js API詳解之 string_decoder用法實例分析

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

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

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

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

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

目錄:

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

new StringDecoder([encoding])

說明:

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

stringDecoder.write(buffer)

說明:

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

demo:

const decoder = new StringDecoder('utf8');
 
//字符的16進制小于0x80屬于單字節(jié)
let outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoqiang
 
//字符的16進制大于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é)會被替換成符合字符編碼的字符
如果提供了 buffer 參數(shù),則在返回剩余字節(jié)之前會再執(zhí)行一次 stringDecoder.write()

demo:

const decoder = new StringDecoder('utf8');
 
//字符的16進制小于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程序設計有所幫助。

相關(guān)文章

最新評論

邵武市| 葫芦岛市| 蓬莱市| 平果县| 宁津县| 临洮县| 玉门市| 浙江省| 新源县| 白城市| 岚皋县| 南皮县| 洛扎县| 咸阳市| 广东省| 兴仁县| 平原县| 常熟市| 濉溪县| 漾濞| 华安县| 日喀则市| 陕西省| 高邮市| 平定县| 绥德县| 江达县| 正镶白旗| 项城市| 盘山县| 云霄县| 白玉县| 澄城县| 杂多县| 恩平市| 汕头市| 扬中市| 郓城县| 安徽省| 桐梓县| 桐梓县|