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

清空信息
关闭评论
vuduchoan
 vuduchoan 2019-02-11 18:18回复
#77
<a href="http://www.cnovas.com/nike-free-inneva-woven-gr%C3%A5-tan-skoh">nike free inneva woven gr氓 tan</a> <a href="http://www.omnicitycorp.com/air-jordan-retro-29-bleu-oranger-chaussuresh">air jordan retro 29 bleu oranger</a> <a href="http://www.cmcheechalk.com/mitchell-and-ness-detroit-red-wings-snapback-hatl">mitchell and ness detroit red wings snapback</a> <a href="http://www.armandopaz.com/ua-micro-g-torch-noir-violet-chaussuresh">ua micro g torch noir violet</a> <a href="http://www.alrightdesign.com/air-jordan-cp3-blanco-zapatosk">air jordan cp3 blanco</a> <a href="http://www.parksindust.com/nike-roshe-run-iv-mens-orange-shoesi">nike roshe run iv mens orange</a>
vuduchoan http://www.vuduchoan.com/
divyangmart
 divyangmart 2019-02-11 02:33回复
#76
<a href="http://www.atolyekarinca.com/nike-kd-8-mens-red-silver-runningf">nike kd 8 mens red silver</a> <a href="http://www.dfarrie.com/zdeno-chara-33-boston-bruins-reebok-premier-alternate-jersey-small-nflv">zdeno chara 33 boston bruins reebok premier alternate jersey small</a> <a href="http://www.mwsuperstore.com/nike-air-jordan-6-slam-dunk-trainersa">nike air jordan 6 slam dunk</a> <a href="http://www.shanamorgan.com/nike-dynamo-free-ps-barn-brun-svart-scarped">nike dynamo free ps barn brun svart</a> <a href="http://www.crisgarrett.com/nike-flynit-air-max-dam%C3%A4nner-gold-rot-kurpesd">nike flynit air max dam盲nner gold rot</a> <a href="http://www.rejuvmedia.com/genuine-ray-ban-sunglasses-uk-sunglassesd">genuine ray ban sunglasses uk</a>
divyangmart http://www.divyangmart.com/
spotmygadget
 spotmygadget 2019-02-10 18:11回复
#75
<a href="http://www.shoutwithus.com/mujeres-asics-gel-kinsei-5-armada-butyf">mujeres asics gel kinsei 5 armada</a> <a href="http://www.vigodog.com/nike-lunarglide-maschio-8.5-scarpea">nike lunarglide maschio 8.5</a> <a href="http://www.brandsdh.com/nike-free-5.0-arancia-xp-schuheb">nike free 5.0 arancia xp</a> <a href="http://www.colorngrey.com/nike-air-max-tailwind-5-dam%C3%A4nner-wei%C3%9F-gr%C3%BCn-butye">nike air max tailwind 5 dam盲nner wei脽 gr眉n</a> <a href="http://www.leviandval.com/kvinners-nike-free-run-4.0-hvit-lilla-butye">kvinners nike free run 4.0 hvit lilla</a> <a href="http://www.naomisbooks.com/zx-flux-gr%C3%A5-rosa-obuve">zx flux gr氓 rosa</a>
spotmygadget http://www.spotmygadget.com/
yeliseeva
 yeliseeva 2019-02-10 06:50回复
#74
<a href="http://www.tmzred.com/air-jordan-retro-5-gr%C3%B8n-hvid-skoi">air jordan retro 5 gr酶n hvid</a> <a href="http://www.alcovozov.com/kvinners-nike-air-max-90-vt-oransje-r%C3%B8d-obuvq">kvinners nike air max 90 vt oransje r酶d</a> <a href="http://www.ahmztradefx.com/air-yeezy-ii-2-sp-max-90-womens-green-red-shoesh">air yeezy ii 2 sp max 90 womens green red</a> <a href="http://www.isampled.com/nike-air-max-thea-viola-nero-scarpei">nike air max thea viola nero</a> <a href="http://www.aldervets.com/nike-sb-stefan-janoski-max-silber-himmelblau-schuhej">nike sb stefan janoski max silber himmelblau</a> <a href="http://www.vanltl.com/kentucky-wildcats-nike-ncaa-pigment-dye-bucket-hath">kentucky wildcats nike ncaa pigment dye bucket</a>
[url=http://www.yeliseeva.com/]yeliseeva[/url]
parsoniidrs
 parsoniidrs 2019-02-09 06:08回复
#73
<a href="http://www.jindianhua.com/nike-free-balanza-argento-marrone-scarpec">nike free balanza argento marrone</a> <a href="http://www.kewauneecares.com/nike-youth-adrian-phillips-elite-electric-blue-alternate-jersey-los-angeles-chargers-nfl-31-vapor-untouchable-nfls">nike youth adrian phillips elite electric blue alternate jersey los angeles chargers nfl 31 vapor untouchable</a> <a href="http://www.losvalenzuela.com/nike-flyknit-lunar-1-gr%C3%B8n-mountain-scarpeg">nike flyknit lunar 1 gr酶n mountain</a> <a href="http://www.redrockrose.com/nike-roshe-run-bl%C3%A5-anthracite-obuvc">nike roshe run bl氓 anthracite</a> <a href="http://www.renightmare.com/kobe-9-low-em-for-venta-scarpea">kobe 9 low em for venta</a> <a href="http://www.topibundle.com/air-jordan-cp3-ix-silber-gold-schuhec">air jordan cp3 ix silber gold</a>
parsoniidrs http://www.parsoniidrs.com/
dixiepundit
 dixiepundit 2019-02-08 06:55回复
#72
<a href="http://www.divyangmart.com/winnipeg-jets-hat-navy-hatd">winnipeg jets hat navy</a> <a href="http://www.ezellhvac.com/nike-free-run-price-philippines-runninge">nike free run price philippines</a> <a href="http://www.oldoyopark.com/nike-air-max-2017-leder-rot-gelb-schuhea">nike air max 2017 leder rot gelb</a> <a href="http://www.racquetlady.com/new-blance-996-ni%C3%B1os-p%C3%BArpura-amarillo-scarpeb">new blance 996 ni帽os p煤rpura amarillo</a> <a href="http://www.ankaralokanta.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.mengenmetal.com/nike-kobe-5-gul-gr%C3%A5-kurpesc">nike kobe 5 gul gr氓</a>
dixiepundit http://www.dixiepundit.com/
temumangga
 temumangga 2019-02-08 00:50回复
#71
<a href="http://www.dronesofusa.com/air-max-thea-light-bl%C3%A5-hvit-print-schuheg">air max thea light bl氓 hvit print</a> <a href="http://www.eonkraftwerke.com/nike-free-hypervenom-mid-azul-graphite-scarpeg">nike free hypervenom mid azul graphite</a> <a href="http://www.inpaetest.com/ua-clutchfit-prodigy-orange-brun-kurpesf">ua clutchfit prodigy orange brun</a> <a href="http://www.lifepakused.com/nike-free-flyknit-4.0-dam%C3%A4nner-zappos-schuheg">nike free flyknit 4.0 dam盲nner zappos</a> <a href="http://www.offshoretent.com/air-jordan-11-rot-october-schuhec">air jordan 11 rot october</a> <a href="http://www.tmtfiltre.com/nike-blazer-dam%C3%A4nner-violett-chaussurese">nike blazer dam盲nner violett</a>
temumangga http://www.temumangga.com/
dxnkofe
 dxnkofe 2019-02-07 18:22回复
#70
<a href="http://www.adanastarpen.com/air-jordan-retro-9-hvit-rosa-obuvi">air jordan retro 9 hvit rosa</a> <a href="http://www.actconfident.com/kvinners-nike-kobe-9-low-gull-bl%C3%A5-obuvp">kvinners nike kobe 9 low gull bl氓</a> <a href="http://www.erzurumihl.com/nike-zoom-pegasus-32-gull-oransje-obuvi">nike zoom pegasus 32 gull oransje</a> <a href="http://www.candctowingil.com/nike-kobe-9-low-mujeres-negro-cielo-azul-zapatosp">nike kobe 9 low mujeres negro cielo azul</a> <a href="http://www.onlysidekick.com/coach-bandit-hobo-39-coachh">coach bandit hobo 39</a> <a href="http://www.rmmassage.com/air-jordan-retro-6-womens-red-black-shoesj">air jordan retro 6 womens red black</a>
dxnkofe http://www.dxnkofe.com/
air jordan 2 low red
 air jordan 2 low red 2019-02-05 21:27回复
#69
<a href="http://www.lorincutts.com/nike-free-4.0-flyknit-schwarz-multicolor-schuhek">nike free 4.0 flyknit schwarz multicolor</a> <a href="http://www.aquabboutique.com/hvid-and-r%C3%B8d-nike-air-max-90-skoi">hvid and r酶d nike air max 90</a> <a href="http://www.shadesnstyles.com/nike-cortez-femmes-rose-quartz-chaussuresh">nike cortez femmes rose quartz</a> <a href="http://www.tlahuilcalli.com/camo-costa-hat-with-orange-logo-hatl">camo costa hat with orange logo</a> <a href="http://www.onewithdrmatt.com/adidas-nitrocharge-1.0-noir-pack-review-chaussuresh">adidas nitrocharge 1.0 noir pack review</a> <a href="http://www.kammtherapy.com/mujer-nike-free-rosado-negro-zapatosk">mujer nike free rosado negro</a>
air jordan 2 low red http://www.thehugdealer.com/air-jordan-2-low-red-shoesj
olafhuth
 olafhuth 2019-02-05 21:08回复
#68
<a href="http://www.georgesherry.com/dam%C3%A4nner-air-jordan-retro-11-schwarz-gr%C3%BCn-schuheb">dam盲nner air jordan retro 11 schwarz gr眉n</a> <a href="http://www.jenalgroup.com/adidas-jets-8-teemu-selanne-camo-authentic-2017-veterans-day-stitched-nhl-jersey-nflu">adidas jets 8 teemu selanne camo authentic 2017 veterans day stitched nhl jersey</a> <a href="http://www.badacamshotel.com/nike-cortez-gold-metallic-skou">nike cortez gold metallic</a> <a href="http://www.compressmyjs.com/new-balance-577-mens-blue-white-runninge">new balance 577 mens blue white</a> <a href="http://www.fetison.com/uk-trainers-nike-flyknit-racer-fire-rooster-zapatosu">uk trainers nike flyknit racer fire rooster</a> <a href="http://www.neffloot.com/kvinners-nike-free-tr-flyknit-5.0-gr%C3%A5-r%C3%B8d-chaussuresa">kvinners nike free tr flyknit 5.0 gr氓 r酶d</a>
olafhuth http://www.olafhuth.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸