JS GB2312与UTF8之间的转换
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /*
2 * GB2312转UTF8
3 * 例:
4 * var xx=new GB2312UTF8();
5 * var Utf8=xx.Gb2312ToUtf8("你aaa好aaaaa");
6 * var Gb2312=xx.Utf8ToGb2312(Utf8);
7 * alert(Gb2312);
8 */
9
10 function GB2312UTF8(){
11 this.Dig2Dec=function(s){
12 var retV = 0;
13 if(s.length == 4){
14 for(var i = 0; i < 4; i ++){
15 retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
16 }
17 return retV;
18 }
19 return -1;
20 }
21 this.Hex2Utf8=function(s){
22 var retS = "";
23 var tempS = "";
24 var ss = "";
25 if(s.length == 16){
26 tempS = "1110" + s.substring(0, 4);
27 tempS += "10" + s.substring(4, 10);
28 tempS += "10" + s.substring(10,16);
29 var sss = "0123456789ABCDEF";
30 for(var i = 0; i < 3; i ++){
31 retS += "%";
32 ss = tempS.substring(i * 8, (eval(i)+1)*8);
33 retS += sss.charAt(this.Dig2Dec(ss.substring(0,4)));
34 retS += sss.charAt(this.Dig2Dec(ss.substring(4,8)));
35 }
36 return retS;
37 }
38 return "";
39 }
40 this.Dec2Dig=function(n1){
41 var s = "";
42 var n2 = 0;
43 for(var i = 0; i < 4; i++){
44 n2 = Math.pow(2,3 - i);
45 if(n1 >= n2){
46 s += '1';
47 n1 = n1 - n2;
48 }
49 else
50 s += '0';
51 }
52 return s;
53 }
54
55 this.Str2Hex=function(s){
56 var c = "";
57 var n;
58 var ss = "0123456789ABCDEF";
59 var digS = "";
60 for(var i = 0; i < s.length; i ++){
61 c = s.charAt(i);
62 n = ss.indexOf(c);
63 digS += this.Dec2Dig(eval(n));
64 }
65 return digS;
66 }
67 this.Gb2312ToUtf8=function(s1){
68 var s = escape(s1);
69 var sa = s.split("%");
70 var retV ="";
71 if(sa[0] != ""){
72 retV = sa[0];
73 }
74 for(var i = 1; i < sa.length; i ++){
75 if(sa[i].substring(0,1) == "u"){
76 retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1,5)));
77 if(sa[i].length){
78 retV += sa[i].substring(5);
79 }
80 }
81 else{
82 retV += unescape("%" + sa[i]);
83 if(sa[i].length){
84 retV += sa[i].substring(5);
85 }
86 }
87 }
88 return retV;
89 }
90 this.Utf8ToGb2312=function(str1){
91 var substr = "";
92 var a = "";
93 var b = "";
94 var c = "";
95 var i = -1;
96 i = str1.indexOf("%");
97 if(i==-1){
98 return str1;
99 }
100 while(i!= -1){
101 if(i<3){
102 substr = substr + str1.substr(0,i-1);
103 str1 = str1.substr(i+1,str1.length-i);
104 a = str1.substr(0,2);
105 str1 = str1.substr(2,str1.length - 2);
106 if(parseInt("0x" + a) & 0x80 == 0){
107 substr = substr + String.fromCharCode(parseInt("0x" + a));
108 }
109 else if(parseInt("0x" + a) & 0xE0 == 0xC0){ //two byte
110 b = str1.substr(1,2);
111 str1 = str1.substr(3,str1.length - 3);
112 var widechar = (parseInt("0x" + a) & 0x1F) << 6;
113 widechar = widechar | (parseInt("0x" + b) & 0x3F);
114 substr = substr + String.fromCharCode(widechar);
115 }
116 else{
117 b = str1.substr(1,2);
118 str1 = str1.substr(3,str1.length - 3);
119 c = str1.substr(1,2);
120 str1 = str1.substr(3,str1.length - 3);
121 var widechar = (parseInt("0x" + a) & 0x0F) << 12;
122 widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
123 widechar = widechar | (parseInt("0x" + c) & 0x3F);
124 substr = substr + String.fromCharCode(widechar);
125 }
126 }
127 else {
128 substr = substr + str1.substring(0,i);
129 str1= str1.substring(i);
130 }
131 i = str1.indexOf("%");
132 }
133
134 return substr+str1;
135 }
136 }