utf-8转为GB2312

来源:互联网 发布:通常所说的linux是 编辑:程序博客网 时间:2024/04/30 20:55

/UTF字符转换
var UTFTranslate = {
Change:function(pValue){
return pValue.replace(/[^\u0000-\u00FF]/g,function($0){returnescape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});
},
ReChange:function(pValue){
return(pValue.replace(/&#x/g,'%u').replace(/\\u/g,'%u').replace(/;/g,''));
}
};

//Asc转换
var AscTranslate = {
Change: function(pAscString, pBoxId){

box = document.getElementByIdx_x(pBoxId);
box.innerHTML = '';
for(i = 0; i < pAscString.length; i++)
{
var asc = pAscString.charCodeAt(i);
var str = String.fromCharCode(asc);
box.innerHTML += str + ':' + asc +'<br/>';
}
}
}

java
byte [] b;
String utf8_value;
utf8_value =request.getParameter("NAME");//从HTTP流中取"NAME"的UTF8数据
b = utf8_value.getBytes("8859_1"); //中间用ISO-8859-1过渡
String name = new String(b, "GB2312"); //转换成GB2312字符

这是我做的一个项目程序的一段:
     byte[] b;
     String gbk_value;
     gbk_value=request.getParameter("address");//从HTTP流中取"name"的GBK数据(由于web.xml中过滤器设置默认编码为GBK,所以外网从UTF-8变为GBK)
     b=gbk_value.getBytes("GBK");//中间用GBK过渡,从GBK转换成GBK数组
     String address=new String(b,"utf-8");//转换成utf-8字符
     myform.setAddress(address);

0 0
原创粉丝点击