/*
*全角空格为12288,半角空格为32
*其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
*/
function SBC(text) {
return text.replace(/[x20-x7e]/g, function($) {
return $ == " " ? "u3000" : String.fromCharCode($.charCodeAt(0) + 0xfee0);
});
}
var toDBC=function(input) {//to DBC case
var res="",c;
for(var i=0;i< input.length;i++) {
c=input.charCodeAt(i);
if(c >=0xFF01 && c <=0xFF5E)// 65281 65374
res+=String.fromCharCode(c-0xFEE0);//65248
else if(c==0x3000)//12288
res+=String.fromCharCode(0x20);//32
else
res+=input.charAt(i);
}
return res;
}
var toSBC=function(input) {//to SBC case
var res="",c;
for(var i=0; i < input.length;i++) {
c=input.charCodeAt(i);
if(c>=0x21 && c<=0x7e)//33 126
res+=String.fromCharCode(c+0xFEE0);
else if(c==0x20)
res += String.fromCharCode(0x3000);
else
res += input.charAt(i);
}
return res;
}
var s=",哦哦,hello ssss!#~";
var s1=toDBC(s);
var s2=toSBC(s1);
alert("原字符:"+s+"nntoDBC:"+s1+"nntoSBC:"+s2);