javascript 实现utf8到gb2312的编码 以及字符的utf-8编码

//转化成十六进制, 调用的是vbscript的Hex函数 function Hex(n) { c = n; execScript("c = Hex(c)", "vbscript"); return c; } //返回文字的AscaII编码, 调用的是vbscript的Asc函数 function Asc(s) { c = s; execScript("c = Asc(c)", "vbscript"); return c; } //获取文字的gb2312编码 function gb2312Encode(str) { var string = ""; c = s = ""; var high = ""; var low = ""; for(var i = 0; i < str.length; i++) { c = Asc(str.charAt(i)); if(Math.abs(c) < 0xFF) string += str.charAt(i); else { if(c < 0) c += 0x10000; high = ((c & 0xFF00) >> 8) & 0x00FF; low = c & 0xFF; string += "%" + Hex(high) + "%" + Hex(low); } } return string; } //将收到的gb2312编码进行解码 function gb2312Decode(data) { string = ""; str = ""; d = data; n = ""; c = ""; execScript("l = LenB(d)", "vbscript"); execScript("d = MidB(d, 1)", "vbscript"); for( i = 1; i <= l; i++) { execScript("c = AscB(MidB(d, i, 1))", "vbscript"); if( c < 0x80) { execScript("str = Chr(c)", "vbscript"); string += str; } else { execScript("n = AscB(MidB(d, i + 1, 1))", "vbscript"); execScript("str = Chr(CLng(c) * &H100 + CInt(n))", "vbscript"); string += str; i = i + 1; } } return string; }

//将收到的gb2312编码转换成对应的文字 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 < j ) { 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("%")); } //对文字进行utf-8编码 function utf8(wide) { var c, s; var enc = ""; var i = 0; while(i<wide.length) { c= wide.charCodeAt(i++); // handle UTF-16 surrogates if (c>=0xDC00 && c<0xE000) continue; if (c>=0xD800 && c<0xDC00) { if (i>=wide.length) continue; s= wide.charCodeAt(i++); if (s<0xDC00 || c>=0xDE00) continue; c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000; } // output value if (c<0x80) enc += String.fromCharCode(c); else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F)); else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F)); else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+ (c&0x3F)); } return enc; } var hexchars = "0123456789ABCDEF"; function toHex(n) { return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF); } var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"; function encodeURIComponentNew(s) { var s = utf8(s); var c; var enc = ""; for(var i= 0; i < s.length; i++) { if(okURIchars.indexOf(s.charAt(i))==-1) enc += "%"+toHex(s.charCodeAt(i)); else enc += s.charAt(i); } return enc; }

你可能感兴趣的:(JavaScript,c,String,function,VBScript,hex)