在 win下child_process模块的exec 返回乱码的问题
发布于 11 年前 作者 llj732589025 8879 次浏览 最后一次编辑是 8 年前

首先按照 iconv-lite 模块 ’npm install iconv-lite’ 先上代码吧: ’‘String.prototype.each=function(i,fun){ var index=0; var that=this; while(index<=that.length){ (fun||function(){})(that.substr(index,i)) index+=i; } } var exec=require(‘child_process’).exec; var iconv=require(’./iconv-lite’); exec(‘ipconfig /all’,{encoding:‘hex’},function(e,r){ var arr=[]; r.each(2,function(data){ arr.push(parseInt(data,16));
}); console.log(iconv.decode(new Buffer(arr), ‘GBK’)) });’' 1 首先把输出的为hex编码的字符串 2 然后把hex 转换为 buffer 3 用iconv-lite 模块把buffer 用gbk模式输出

ps:如果大家有什么好的方法 请告诉我

2 回复

‘markdom’ 失败

String.prototype.each=function(i,fun){
  var index=0;
  var that=this;
  while(index<=that.length){
    (fun||function(){})(that.substr(index,i))
      index+=i;
  }
}
var exec=require('child_process').exec;
var iconv=require('./iconv-lite');
exec('ipconfig /all',{encoding:'hex'},function(e,r){
   var arr=[];
   r.each(2,function(data){
       arr.push(parseInt(data,16));
   });
   console.log(iconv.decode(new Buffer(arr), 'GBK'))
});
回到顶部