JS 从unicode转换到GB

网上有很多js代码从GB转换为unicode,但是js读cookies时读到的是unicode,现显示中文只好用查表法实现

var  str;
str
= " %D6%D0%B9%FA%B3%CC%D0%F2%D4%B1%B4%F3%B1%BE%D3%AA+%3c%3e%22%23%25%7b%7d%7c%5e%7e%5b%5d%60%26%3f%2bAbc " ;   
alert(URLDecode(str)); 

function  URLDecode(enStr)
    
var deStr,strSpecial   
    
var c,i,v,q,p,t,s;
    deStr
="";  
    strSpecial
="!\"#$%&'()*+,.-_/:;<=>?@[\]^`{|}~%";  
    for(i=0;i<enStr.length;i++){  
        c=enStr.substr(i,1);    
        if (c==
"%"){
            q=enStr.substr(i+1,2);
            v=eval(
"0x"+enStr.substr(i+1,2));
            p=String.fromCharCode(v);
            if(strSpecial.indexOf(p,0)!=-1){
                deStr=deStr+p;
                i=i+2;
            }
            else{
                q=enStr.substr(i+1,2);
                t=eval(
"0x"+q)-0xB0;
                q=enStr.substr(i+4,2);
                s=eval(
"0x"+q)-0xA1;
                if(t>=0 && s>=0){
                    v=t*94+s;
                    p=strGB.substr(v,1); 
                    //v=eval(
"0x"+enStr.substr(i+1,2)+enStr.substr(i+4,2));
                    //p=String.fromCharCode(v);
                    deStr=deStr+p;
                    i=i+5;
                }
                else{
                    v=eval(
"0x"+enStr.substr(i+1,2));
                    p=String.fromCharCode(v);
                    deStr=deStr+p;
                    i=i+2;
                }
            }
        }
        else{  
            if(c==
"+"){  
                deStr=deStr+
" ";
            }
            else{
                deStr=deStr+c;
            }   
        }   
    }  
    return deStr;   
}
使用前需要qswhGB2312.js这个文件支持。 http://files.cnblogs.com/feishu/qswhGB2312.js

你可能感兴趣的:(unicode)