好!欢迎访问迷津渡口 现在时间是: 天气 °C ~ °C

迷津渡口

越感到迷惑 越接近真理

nodejs中gbk转码为utf8,以exec('dir')为例

最近从Linux换为Windows,因此开始需要考虑gbk转码的问题。

nodejs中转码需要注意三个问题:

  1. 读取时不能使用默认utf8转码,需使用二进制即 `binary`编码 

  2. 采用iconv-lite模块实际转码

  3. 转换为其他编码之前需要使用原编码解码成功方可!!!

安装iconv-lite:

npm i iconv-lite -S

以exec('dir')为例

// 引入http,iconv-lite和exec模块
const http = require('http');
const iconv = require('iconv-lite');
const { exec } = require('child_process');

http.createServer(function(request, response) {
    //执行dir命令病设置编码为二进制编码 binary
    exec('dir', { encoding: 'binary' }, function(error, stdout, stderr) {
        // 处理结果,转码为utf8
        // 第一步:获取buffer,设为const避免误修改
        const bufferReaded = Buffer.from(stdout, 'binary');
        // 第二步:根据原来的编码gbk解码
        let strGbk = iconv.decode(bufferReaded, 'gbk');
        // 第三步:转为utf8 buffer
        const strUTF8Buffer = iconv.encode(strGbk, 'utf8');
        // 第四步:转为utf8 字符串
        // 多种方法:
        // iconv.decode(strBuf,'utf8')或者toString('utf8')
        let strUTF8 = iconv.decode(strUTF8Buffer, 'utf8');
        // toString()方法
        // let strUTF8 = strUTF8Buffer.toString('utf8');

        response.writeHead(200, { 'Content-Type': 'text/plain;charset=utf8' });
        // 输出转换完成信息
        response.end(strUTF8);
        // 实际上可以直接在这里输出utf8编码的buffer,http会自动转换利用toString转换,默认编码为utf8
        // response.end(strUTF8Buffer);
    });
}).listen(9527, () => {
    console.log('服务器地址为:http://127.0.0.1:9527');
});

临时修改终端编码的方法

配置成utf8编码:

chcp 65001

将终端换成UTF-8代码页,配置后cmd命令的输出就是utf8编码,无需再转码了。


 恢复GBK编码:

chcp 936

有任何好方法欢迎交流~

点赞18
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
graciecomm
 graciecomm 约 2 小时前回复
#190
<a href="http://www.ptsbarwinslow.com/kevin-durant-the-city-warriors-jersey-nfli">kevin durant the city warriors jersey</a> <a href="http://www.sarhallafk.com/nike-air-force-1-high-07-lv8-flax-outdoor-verde-zapatosh">nike air force 1 high 07 lv8 flax outdoor verde</a> <a href="http://www.philippikus.com/adidas-zx-750-gris-verde-zapatosk">adidas zx 750 gris verde</a> <a href="http://www.pngoilgas.com/north-face-mens-gore-tex-pro-shell-jacket-grey-northfaceh">north face mens gore tex pro shell jacket grey</a> <a href="http://www.goficarwash.com/nike-foamposite-rot-schwarz-schuhek">nike foamposite rot schwarz</a> <a href="http://www.getairtijuana.com/le-coq-sportif-m%C3%A6nd-sort-hvid-2-skoi">le coq sportif m忙nd sort hvid 2</a>
graciecomm http://www.graciecomm.com/
datauice
 datauice 2019-04-24 20:47回复
#189
<a href="http://www.cosmocrafters.com/nike-air-max-2017-dam%C3%A4nner-orange-blau-kurpesf">nike air max 2017 dam盲nner orange blau</a> <a href="http://www.dschocolate.com/asics-gel-kayano-20-argent-rouge-kurpesa">asics gel kayano 20 argent rouge</a> <a href="http://www.eucbowl.com/mujeres-air-jordan-retro-13-negro-blanco-chaussuresb">mujeres air jordan retro 13 negro blanco</a> <a href="http://www.gartnerhrinc.com/air-jordan-retro-5-ni%C3%B1os-naranja-obuve">air jordan retro 5 ni帽os naranja</a> <a href="http://www.akkadixllc.com/nike-kd-7-bianca-and-oro-games-chaussuresa">nike kd 7 bianca and oro games</a> <a href="http://www.beertapmap.com/nike-kobe-5-argento-arancia-butya">nike kobe 5 argento arancia</a>
datauice http://www.datauice.com/
blovese
 blovese 2019-04-24 20:10回复
#188
<a href="http://www.unefinemouche.com/north-face-jacket-blue-jd-northfaceh">north face jacket blue jd</a> <a href="http://www.ayuinc.com/dam%C3%A4nner-nike-sb-stefan-janoski-max-orangen-gold-schuhej">dam盲nner nike sb stefan janoski max orangen gold</a> <a href="http://www.unirlisboa.com/adidas-neo-canvas-r%C3%B8d-lilla-skoh">adidas neo canvas r酶d lilla</a> <a href="http://www.escortfeeds.com/adidas-tubular-runner-jaune-gris-chaussuresh">adidas tubular runner jaune gris</a> <a href="http://www.rdjanitorial.com/san-diego-padres-new-era-mlb-team-out-heather-59fifty-cap-hatl">san diego padres new era mlb team out heather 59fifty cap</a> <a href="http://www.bookoobeats.com/nike-kd-8-hommes-gris-chaussuresh">nike kd 8 hommes gris</a>
blovese http://www.blovese.com/
bewustvitaal
 bewustvitaal 2019-04-23 09:52回复
#187
<a href="http://www.sugarforspice.com/nike-roshe-hyper-breathe-pink-shoesi">nike roshe hyper breathe pink</a> <a href="http://www.mybancorphsa.com/nike-air-max-bw-s%C3%B8lv-himmelbl%C3%A5-skoj">nike air max bw s酶lv himmelbl氓</a> <a href="http://www.scopatore.com/nike-shox-svart-rosa-obuvi">nike shox svart rosa</a> <a href="http://www.galacticlogos.com/nike-air-max-janoski-svart-hvit-obuvp">nike air max janoski svart hvit</a> <a href="http://www.ispardmore.com/nike-air-max-zero-kvinners-hvit-gull-obuvi">nike air max zero kvinners hvit gull</a> <a href="http://www.radioramabc.com/air-jordan-retro-1-mujeres-cielo-azul-gris-zapatosp">air jordan retro 1 mujeres cielo azul gris</a>
bewustvitaal http://www.bewustvitaal.com/
skonya
 skonya 2019-04-22 10:40回复
#186
<a href="http://www.taiwanmeinet.com/dam%C3%A4nner-nike-air-max-1-grau-schuheh">dam盲nner nike air max 1 grau</a> <a href="http://www.talkwithgirl.com/adidas-mens-footwear-adidas-neo-skate-gold-white-shoesk">adidas mens footwear adidas neo skate gold white</a> <a href="http://www.mamatale.com/adidas-stan-smith-one-piece-strap-vapor-lyser%C3%B8d-skol">adidas stan smith one piece strap vapor lyser酶d</a> <a href="http://www.strongtale.com/air-jordan-retro-2-gr%C3%B8nn-svart-fluorescent-gr%C3%B8nn-obuvk">air jordan retro 2 gr酶nn svart fluorescent gr酶nn</a> <a href="http://www.iranpayamco.com/nike-mercurial-superfly-bianca-nero-volt-total-arancia-scarpei">nike mercurial superfly bianca nero volt total arancia</a> <a href="http://www.hapeid.com/asics-gel-kinsei-4-mujer-rosado-azul-zapatos-zapatosh">asics gel kinsei 4 mujer rosado azul zapatos</a>
skonya http://www.skonya.com/
ipodammo
 ipodammo 2019-04-22 08:39回复
#185
<a href="http://www.gaiaescort.com/north-face-bubble-jacket-womens-northfaceh">north face bubble jacket womens</a> <a href="http://www.hatbest.com/nike-air-max-2017-dam%C3%A4nner-gelb-rosa-schuhek">nike air max 2017 dam盲nner gelb rosa</a> <a href="http://www.playcheater.com/air-max-2013-sort-and-gul-skoi">air max 2013 sort and gul</a> <a href="http://www.alanyalinks.com/adidas-zx-flux-xeno-reflective-noir-sqplz5158-chaussuresh">adidas zx flux xeno reflective noir sqplz5158</a> <a href="http://www.mxclimited.com/new-era-hats-manufacturing-hatl">new era hats manufacturing</a> <a href="http://www.ueaydin.com/adidas-zx-flux-snake-print-noir-chaussuresh">adidas zx flux snake print noir</a>
ipodammo http://www.ipodammo.com/
batemanafrica
 batemanafrica 2019-04-22 07:59回复
#184
<a href="http://www.nhhairdesign.com/nike-air-yeezy-2-rot-october-bazar-obuvc">nike air yeezy 2 rot october bazar</a> <a href="http://www.struysthuis.com/kvinners-nike-air-max-90-rosa-gul-chaussuresg">kvinners nike air max 90 rosa gul</a> <a href="http://www.golfcavedave.com/nike-free-3.0-v4-brun-guld-chaussuresa">nike free 3.0 v4 brun guld</a> <a href="http://www.yfhfoundation.com/nike-cortez-sort-n-hvid-butya">nike cortez sort n hvid</a> <a href="http://www.exoticbg.com/adidas-superstar-b%C3%B8rn-sort-butyd">adidas superstar b酶rn sort</a> <a href="http://www.sushantnayak.com/air-jordan-1-retro-high-sort-cyber-bl%C3%A5-sapphire-scarpeg">air jordan 1 retro high sort cyber bl氓 sapphire</a>
[url=http://www.batemanafrica.com/]batemanafrica[/url]
dynamixawards
 dynamixawards 2019-04-21 19:20回复
#183
<a href="http://www.taiwanmeinet.com/kvinner-adidas-zx-630-marinen-hvit-r%C3%B8d-obuvi">kvinner adidas zx 630 marinen hvit r酶d</a> <a href="http://www.talkwithgirl.com/mujer-nike-kyrie-3-negro-blanco-zapatosl">mujer nike kyrie 3 negro blanco</a> <a href="http://www.mamatale.com/nike-free-5.0-femminile-blu-bianca-scarpeh">nike free 5.0 femminile blu bianca</a> <a href="http://www.strongtale.com/nike-air-max-tailwind-8-blue-shoesk">nike air max tailwind 8 blue</a> <a href="http://www.iranpayamco.com/ray-ban-clubmaster-aluminum-copper-sunglassesh">ray ban clubmaster aluminum copper</a> <a href="http://www.hapeid.com/jaune-noir-hommes-air-jordan-retro-14-chaussures-chaussuresl">jaune noir hommes air jordan retro 14 chaussures</a>
dynamixawards http://www.dynamixawards.com/
slotoyunpro
 slotoyunpro 2019-04-21 14:53回复
#182
<a href="http://www.ptsbarwinslow.com/chicago-bears-47-nfl-47-clean-up-cap-hath">chicago bears 47 nfl 47 clean up cap</a> <a href="http://www.sarhallafk.com/femmes-nike-air-max-thea-vert-rose-chaussuresh">femmes nike air max thea vert rose</a> <a href="http://www.philippikus.com/frau-nike-internationalist-jcrd-woven-schwarz-rosa-wei%C3%9F-schuheh">frau nike internationalist jcrd woven schwarz rosa wei脽</a> <a href="http://www.pngoilgas.com/nike-kobe-10-sliver-black-shoesk">nike kobe 10 sliver black</a> <a href="http://www.goficarwash.com/m%C3%A6nd-nike-air-max-2017-l%C3%A6der-sort-bl%C3%A5-2-skoj">m忙nd nike air max 2017 l忙der sort bl氓 2</a> <a href="http://www.getairtijuana.com/herren-nike-flyknit-racer-blau-gr%C3%BCn-schuheh">herren nike flyknit racer blau gr眉n</a>
slotoyunpro http://www.slotoyunpro.com/
easytanklevel
 easytanklevel 2019-04-20 20:44回复
#181
<a href="http://www.ikennemer.com/air-max-95-wei%C3%9F-grape-kurpesg">air max 95 wei脽 grape</a> <a href="http://www.inspiresatis.com/nike-lunarepic-flyknit-low-sizing-obuvb">nike lunarepic flyknit low sizing</a> <a href="http://www.matjarkum.com/nike-free-rn-femmes-noir-quarterbacks-butya">nike free rn femmes noir quarterbacks</a> <a href="http://www.milligande.com/nike-limited-steve-largent-jersey-80-nfl-seattle-seahawks-2016-salute-to-service-youth-black-nflu">nike limited steve largent jersey 80 nfl seattle seahawks 2016 salute to service youth black</a> <a href="http://www.mybizcounsel.com/herre-nike-air-max-tailwind-7-orange-r%C3%B8d-kurpesb">herre nike air max tailwind 7 orange r酶d</a> <a href="http://www.riddercorp.com/nike-air-max-2015-arancia-five-schuhec">nike air max 2015 arancia five</a>
easytanklevel http://www.easytanklevel.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸