javascript converting strings from gbk to utf-8 -


i got text internet node.js , want convert gbk encoding utf-8.

i tried node-iconv module, didn't work.

var iconv = require('iconv').iconv; var gbk_to_utf8 = new iconv('gbk', 'utf-8'); var b = gbk_to_utf8.convert(new buffer(body.tostring())); console.log(b.tostring()); 

try code this link:

gb2312utf8  = {   dig2dec : function(s){       var retv = 0;       if(s.length == 4){           for(var = 0; < 4; ++){               retv += eval(s.charat(i)) * math.pow(2, 3 - i);           }           return retv;       }       return -1;   } ,    hex2utf8 : function(s){      var rets = "";      var temps = "";      var ss = "";      if(s.length == 16){          temps = "1110" + s.substring(0, 4);          temps += "10" +  s.substring(4, 10);          temps += "10" + s.substring(10,16);          var sss = "0123456789abcdef";          for(var = 0; < 3; ++){             rets += "%";             ss = temps.substring(i * 8, (eval(i)+1)*8);             rets += sss.charat(this.dig2dec(ss.substring(0,4)));             rets += sss.charat(this.dig2dec(ss.substring(4,8)));          }          return rets;      }      return "";   } ,    dec2dig : function(n1){       var s = "";       var n2 = 0;       for(var = 0; < 4; i++){          n2 = math.pow(2,3 - i);          if(n1 >= n2){             s += '1';             n1 = n1 - n2;           }          else           s += '0';       }       return s;   },    str2hex : function(s){       var c = "";       var n;       var ss = "0123456789abcdef";       var digs = "";       for(var = 0; < s.length; ++){          c = s.charat(i);          n = ss.indexof(c);          digs += this.dec2dig(eval(n));       }       return digs;   },    gb2312toutf8 : function(s1){     var s = escape(s1);     var sa = s.split("%");     var retv ="";     if(sa[0] != ""){       retv = sa[0];     }     for(var = 1; < sa.length; ++){       if(sa[i].substring(0,1) == "u"){         //alert(this.str2hex(sa[i].substring(1,5)));         retv += this.hex2utf8(this.str2hex(sa[i].substring(1,5)));   if(sa[i].length){     retv += sa[i].substring(5);   }       }       else{      retv += unescape("%" + sa[i]);   if(sa[i].length){     retv += sa[i].substring(5);   }    }     }     return retv;   },    utf8togb2312 : function(str1){         var substr = "";         var = "";         var b = "";         var c = "";         var = -1;         = str1.indexof("%");         if(i==-1){           return str1;         }         while(i!= -1){     if(i<3){                 substr = substr + str1.substr(0,i-1);                 str1 = str1.substr(i+1,str1.length-i);                 = str1.substr(0,2);                 str1 = str1.substr(2,str1.length - 2);                 if(parseint("0x" + a) & 0x80 == 0){                   substr = substr + string.fromcharcode(parseint("0x" + a));                 }                 else if(parseint("0x" + a) & 0xe0 == 0xc0){ //two byte                         b = str1.substr(1,2);                         str1 = str1.substr(3,str1.length - 3);                         var widechar = (parseint("0x" + a) & 0x1f) << 6;                         widechar = widechar | (parseint("0x" + b) & 0x3f);                         substr = substr + string.fromcharcode(widechar);                 }                 else{                         b = str1.substr(1,2);                         str1 = str1.substr(3,str1.length - 3);                         c = str1.substr(1,2);                         str1 = str1.substr(3,str1.length - 3);                         var widechar = (parseint("0x" + a) & 0x0f) << 12;                         widechar = widechar | ((parseint("0x" + b) & 0x3f) << 6);                         widechar = widechar | (parseint("0x" + c) & 0x3f);                         substr = substr + string.fromcharcode(widechar);                 }      }      else {       substr = substr + str1.substring(0,i);       str1= str1.substring(i);      }               = str1.indexof("%");         }          return substr+str1;   } }; 

and test function:

gbk => utf8: var utf8 = gb2312utf8.gb2312toutf8("中文gb2312");  utf8 => gbk: gb2312utf8.utf8togb2312(utf8); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -