Home >Web Front-end >JS Tutorial >Two ways to convert js encoding between gb2312 and utf8_javascript skills

Two ways to convert js encoding between gb2312 and utf8_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:26:292242browse

方法一:

复制代码 代码如下:

function gb2utf8(data){
      var glbEncode = [];
      gb2utf8_data = data;
      execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
      var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
      t=t.split("@");
      var i=0,j=t.length,k;
      while( i         k=t[i].substring(0,4);
         if(!glbEncode[k]) 
         {
            gb2utf8_char = eval("0x" k);
            execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
            glbEncode[k]=escape(gb2utf8_char).substring(1,6);
         }
         t[i]=glbEncode[k] t[i].substring(4);
      }
      gb2utf8_data = gb2utf8_char = null;
      return unescape(t.join("%"));
 }
 

 使用时:xxx.innerHTML=gb2utf8(req.responseBody);
方法二:
复制代码 代码如下:


 

在java类中配合使用: (适用于ajax)
复制代码 代码如下:

String dwp = new String(request.getParameter("dwp").getBytes("ISO-8859-1"),"gbk") ;
String dwc = new String(request.getParameter("dwc").getBytes("ISO-8859-1"),"gbk") ;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn