好!欢迎访问迷津渡口 现在时间是: 天气 °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
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
texomatruss
 texomatruss 约 12 小时前回复
#285
<a href="http://www.getfape.com/parajumpers-juliet-jacket-xl-parajumpersa">parajumpers juliet jacket xl</a> <a href="http://www.aimauctions.com/moncler-maya-jacket-navy-2017-monclera">moncler maya jacket navy 2017</a> <a href="http://www.wishsupporter.com/adidas-tubular-runner-primeknit-sort-gr%C3%B8n-chaussuresb">adidas tubular runner primeknit sort gr酶n</a> <a href="http://www.revisionsbacs.com/nike-free-flyknit-5.0-l%C3%B8ping-sko-gray-zapatosj">nike free flyknit 5.0 l酶ping sko gray</a> <a href="http://www.duvetboutique.com/nike-free-rn-flyknit-noir-rock-obuvf">nike free rn flyknit noir rock</a> <a href="http://www.bestmotosport.com/pandora-charms-disney-orlando-tickets-pandoraj">pandora charms disney orlando tickets</a>
texomatruss http://www.texomatruss.com/
modconsol
 modconsol 约 14 小时前回复
#284
<a href="http://www.jlredcross.com/black-fendi-college-belt-beltr">black fendi college belt</a> <a href="http://www.nownessninja.com/mackage-jacket-sale-montreal-phone-number-hours-mackagee">mackage jacket sale montreal phone number hours</a> <a href="http://www.tedsrcrepair.com/hermes-reversible-belt-hermesr">hermes reversible belt</a> <a href="http://www.zovailangnews.com/parajumpers-gobi-mens-parajumpersr">parajumpers gobi mens</a> <a href="http://www.garagesetcinc.com/air-jordan-4.5-marr%C3%B3n-rosado-obuvb">air jordan 4.5 marr贸n rosado</a> <a href="http://www.immoetvous.com/nike-air-max-2017-red-and-white-uniform-runningd">nike air max 2017 red and white uniform</a>
modconsol http://www.modconsol.com/
duracordstyle
 duracordstyle 2019-12-14 12:01回复
#283
<a href="http://www.studiovivalex.com/pandora-charms-ireland-homes-pandorad">pandora charms ireland homes</a> <a href="http://www.italikacink.com/air-jordan-11-dirty-brosso-pictures-trainersi">air jordan 11 dirty brosso pictures</a> <a href="http://www.atomaugers.com/new-balance-574-orange-norway-chaussuresg">new balance 574 orange norway</a> <a href="http://www.nerecords.com/polo-ralph-lauren-sweater-nero-poloa">polo ralph lauren sweater nero</a> <a href="http://www.amuntech.com/nike-lebron-11-basketbtodas-zapatos-scarpee">nike lebron 11 basketbtodas zapatos</a> <a href="http://www.miraklegal.com/sonniges-m%C3%A4dchen-blumen-kleid-dresse">sonniges m盲dchen blumen kleid</a>
[url=http://www.duracordstyle.com/]duracordstyle[/url]
darwinquartet
 darwinquartet 2019-12-14 05:13回复
#282
<a href="http://www.firestonelive.com/canada-goose-jacket-xs-menu-canadagooseb">canada goose jacket xs menu</a> <a href="http://www.joyhaughton.com/adidas-superstar-vintage-deluxe-gr%C3%B8n-kurpesk">adidas superstar vintage deluxe gr酶n</a> <a href="http://www.witorodriguez.com/pandora-charms-colgante-de-coraz-pandoraf">pandora charms colgante de coraz</a> <a href="http://www.nantotel.com/christian-louboutin-lady-glitter-peep-toe-louboutine">christian louboutin lady glitter peep toe</a> <a href="http://www.insideoutelle.com/hermes-birkin-stamp-2017-hermesc">hermes birkin stamp 2017</a> <a href="http://www.montyclarkpi.com/nike-lebron-13-b%C3%B8rn-brun-r%C3%B8d-footm">nike lebron 13 b酶rn brun r酶d</a>
darwinquartet http://www.darwinquartet.com/
introsphera
 introsphera 2019-12-14 03:02回复
#281
<a href="http://www.autosasart.com/mk-rhea-mini-backpack-mkr">mk rhea mini backpack</a> <a href="http://www.centrodunamis.com/herre-asics-gel-noosa-tri-7-gr%C3%B8n-guld-scarpec">herre asics gel noosa tri 7 gr酶n guld</a> <a href="http://www.kideschool.com/parajumpers-leather-jacket-parajumpersr">parajumpers leather jacket</a> <a href="http://www.preloxlady.com/air-jordan-cp3-ix-rosa-wei%C3%9F-chaussuresg">air jordan cp3 ix rosa wei脽</a> <a href="http://www.scalfarogroup.com/canada-goose-kensington-parka-niagara-grape-canadagooser">canada goose kensington parka niagara grape</a> <a href="http://www.yuriboguinia.com/nfl-dallas-cowboys-bendire-pullover-hoodie-black-nflu">nfl dallas cowboys bendire pullover hoodie black</a>
introsphera http://www.introsphera.com/
curlywoodwind
 curlywoodwind 2019-12-13 18:00回复
#280
<a href="http://www.popofred.com/nike-roshe-blau-aztec-scarpee">nike roshe blau aztec</a> <a href="http://www.murekachart.com/adrianna-papell-plus-gebl%C3%BCmtes-kleid-mit-ellenbogen%C3%A4rmeln-dresse">adrianna papell plus gebl眉mtes kleid mit ellenbogen盲rmeln</a> <a href="http://www.naturecaredoc.com/jordan-3-sort-iron-lilla-footc">jordan 3 sort iron lilla</a> <a href="http://www.protowbend.com/air-jordan-retro-5-fire-rot-kicksonfire-butym">air jordan retro 5 fire rot kicksonfire</a> <a href="http://www.twominuteview.com/parajumpers-jakke-dame-menn-parajumpers-ugo-parkas-parajumpersb">parajumpers jakke dame menn parajumpers ugo parkas</a> <a href="http://www.stephenemay.com/canada-goose-constable-qb-online-canadagooseb">canada goose constable qb online</a>
curlywoodwind http://www.curlywoodwind.com/
revisionbacs
 revisionbacs 2019-12-12 18:03回复
#279
<a href="http://www.boscopy.com/parajumpers-vest-xl-yard-parajumpersa">parajumpers vest xl yard</a> <a href="http://www.azyachten.com/moncler-jacket-repair-gold-monclera">moncler jacket repair gold</a> <a href="http://www.agencysync.com/vibram-fivefingers-kso-arancia-rosso-chaussuresb">vibram fivefingers kso arancia rosso</a> <a href="http://www.plushthreads.com/air-jordan-retro-14-rosso-bianca-zapatosj">air jordan retro 14 rosso bianca</a> <a href="http://www.bateslzw.com/nike-roshe-run-i-r%C3%B8d-orange-obuvf">nike roshe run i r酶d orange</a> <a href="http://www.countryabbey.com/pandora-bead-for-aunt-pandoraj">pandora bead for aunt</a>
revisionbacs http://www.revisionbacs.com/
fotoversorger
 fotoversorger 2019-12-09 21:00回复
#278
<a href="http://www.foradocasulo.com/how-to-tie-burberry-cashmere-scarf-scarfr">how to tie burberry cashmere scarf</a> <a href="http://www.madamevelvet.com/ralph-lauren-purple-label-t-shirt-polot">ralph lauren purple label t shirt</a> <a href="http://www.millertiregi.com/nike-internationalist-jcrd-weave-black-pink-white-sportsc">nike internationalist jcrd weave black pink white</a> <a href="http://www.thecoedge.com/nike-air-flight-89-jordan-cement-print-kits-trainerse">nike air flight 89 jordan cement print kits</a> <a href="http://www.vorsch.com/adidas-blackhawks-16-bobby-hull-black-authentic-2019-winter-classic-stitched-nhl-jersey-nflz">adidas blackhawks 16 bobby hull black authentic 2019 winter classic stitched nhl jersey</a> <a href="http://www.accsmonnaux.com/new-balance-996-gr%C3%B8n-lyser%C3%B8d-chaussuresf">new balance 996 gr酶n lyser酶d</a>
fotoversorger http://www.fotoversorger.com/
motykare
 motykare 2019-12-09 09:54回复
#277
<a href="http://www.scrgruppen.com/naranja-polo-sweater-poloa">naranja polo sweater</a> <a href="http://www.jklindfors.com/nike-lebron-13-gs-wolf-gris-print-scarpek">nike lebron 13 gs wolf gris print</a> <a href="http://www.lorenaflowers.com/blumen-kleider-nyc-dresse">blumen kleider nyc</a> <a href="http://www.suppersu.com/air-jordan-melo-m11-grau-orange-footi">air jordan melo m11 grau orange</a> <a href="http://www.riverfresno.com/zx-850-gr%C3%BCn-butyg">zx 850 gr眉n</a> <a href="http://www.blecamerica.com/parajumpers-coat-xs-express-parajumpersb">parajumpers coat xs express</a>
motykare http://www.motykare.com/
sonsofmalcolm
 sonsofmalcolm 2019-12-08 19:00回复
#276
<a href="http://www.limebluspa.com/nike-free-flyknit-4.0-alle-sort-butyg">nike free flyknit 4.0 alle sort</a> <a href="http://www.chardix.com/billig-parajumpers-giuly-parajumpers-mantel-damen-parajumpersb">billig parajumpers giuly parajumpers mantel damen</a> <a href="http://www.mstewartlegal.com/canada-goose-hybridge-lite-hoody-damen-queen-canadagooseb">canada goose hybridge lite hoody damen queen</a> <a href="http://www.dciphone.com/curry-2-r%C3%B8d-and-sort-yeezy-kurpesk">curry 2 r酶d and sort yeezy</a> <a href="http://www.adsdialysis.com/pandora-rings-princess-toy-pandoraf">pandora rings princess toy</a> <a href="http://www.whoismrpink.com/christian-louboutin-pigalle-bianca-and-alti-pumps-edinburgh-louboutine">christian louboutin pigalle bianca and alti pumps edinburgh</a>
sonsofmalcolm http://www.sonsofmalcolm.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸