js 中文字符串转base64

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <title></title>
  5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6 <style type="text/css">
  7 <!--
  8 body{
  9 margin:0px;
 10 padding:0px;
 11 }
 12 body,td{
 13 font-size:9pt;
 14 }
 15 -->
 16 </style>
 17 <script type="text/javascript">
 18 <!--
 19 var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 20 //将Ansi编码的字符串进行Base64编码
 21 function encode64(input) {
 22 var output = "";
 23 var chr1, chr2, chr3 = "";
 24 var enc1, enc2, enc3, enc4 = "";
 25 var i = 0;
 26 do {
 27 chr1 = input.charCodeAt(i++);
 28 chr2 = input.charCodeAt(i++);
 29 chr3 = input.charCodeAt(i++);
 30 enc1 = chr1 >> 2;
 31 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
 32 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
 33 enc4 = chr3 & 63;
 34 if (isNaN(chr2)) {
 35 enc3 = enc4 = 64;
 36 } else if (isNaN(chr3)) {
 37 enc4 = 64;
 38 }
 39 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
 40 + keyStr.charAt(enc3) + keyStr.charAt(enc4);
 41 chr1 = chr2 = chr3 = "";
 42 enc1 = enc2 = enc3 = enc4 = "";
 43 } while (i < input.length);
 44 return output;
 45 }
 46 //将Base64编码字符串转换成Ansi编码的字符串
 47 function decode64(input) {
 48 var output = "";
 49 var chr1, chr2, chr3 = "";
 50 var enc1, enc2, enc3, enc4 = "";
 51 var i = 0;
 52 if (input.length % 4 != 0) {
 53 return "";
 54 }
 55 var base64test = /[^A-Za-z0-9\+\/\=]/g;
 56 if (base64test.exec(input)) {
 57 return "";
 58 }
 59 do {
 60 enc1 = keyStr.indexOf(input.charAt(i++));
 61 enc2 = keyStr.indexOf(input.charAt(i++));
 62 enc3 = keyStr.indexOf(input.charAt(i++));
 63 enc4 = keyStr.indexOf(input.charAt(i++));
 64 chr1 = (enc1 << 2) | (enc2 >> 4);
 65 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
 66 chr3 = ((enc3 & 3) << 6) | enc4;
 67 output = output + String.fromCharCode(chr1);
 68 if (enc3 != 64) {
 69 output += String.fromCharCode(chr2);
 70 }
 71 if (enc4 != 64) {
 72 output += String.fromCharCode(chr3);
 73 }
 74 chr1 = chr2 = chr3 = "";
 75 enc1 = enc2 = enc3 = enc4 = "";
 76 } while (i < input.length);
 77 return output;
 78 }
 79 function utf16to8(str) {
 80 var out, i, len, c;
 81 out = "";
 82 len = str.length;
 83 for(i = 0; i < len; i++) {
 84 c = str.charCodeAt(i);
 85 if ((c >= 0x0001) && (c <= 0x007F)) {
 86 out += str.charAt(i);
 87 } else if (c > 0x07FF) {
 88 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
 89 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
 90 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
 91 } else {
 92 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
 93 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
 94 }
 95 }
 96 return out;
 97 }
 98 function utf8to16(str) {
 99 var out, i, len, c;
100 var char2, char3;
101 out = "";
102 len = str.length;
103 i = 0;
104 while(i < len) {
105 c = str.charCodeAt(i++);
106 switch(c >> 4) {
107 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
108 // 0xxxxxxx
109 out += str.charAt(i-1);
110 break;
111 case 12: case 13:
112 // 110x xxxx 10xx xxxx
113 char2 = str.charCodeAt(i++);
114 out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
115 break;
116 case 14:
117 // 1110 xxxx 10xx xxxx 10xx xxxx
118 char2 = str.charCodeAt(i++);
119 char3 = str.charCodeAt(i++);
120 out += String.fromCharCode(((c & 0x0F) << 12) |
121 ((char2 & 0x3F) << 6) |
122 ((char3 & 0x3F) << 0));
123 break;
124 }
125 }
126 return out;
127 }
128 // 测试代码 开始
129 var de = encode64(utf16to8("你好世界"));
130 document.writeln(de+"<br>");
131 var ee = utf8to16(decode64(de))
132 document.writeln(ee);
133 // 测试代码 结束
134 //-->
135 </script>
136 </head>
137 <body>
138 </body>
139 </html>

 

posted @ 2017-03-22 14:19  黄鹂  阅读(2941)  评论(1编辑  收藏  举报