複製程式碼 程式碼如下: <br>/* utf.js - UTF-8 <=> UTF-16 convertion<br>*<br>* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp><br>* Version: 1.0<br>* LastModified: Dec 25 1999<br>* This library is free. You can redistribute it and/or modify it.<br>*/ <p>/*<br>* Interfaces:<br>* utf8 = utf16to8(utf16);<br>* utf16 = utf16to8(utf8);<br>*/</p> <p>function utf16to8(str) {<br> var out, i, len, c;</p> <p> out = "";<br> len = str.length;<br> for(i = 0; i < len; i++) {<BR> c = str.charCodeAt(i);<BR> if ((c >= 0x0001) && (c <= 0x007F)) {<BR> out += str.charAt(i);<BR> } else if (c > 0x07FF) {<br> out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));<br> out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));<br> out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));<br> } else {<br> out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));<br> out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));<br> }<br> }<br> return out;<br>}</p> <p>function utf8to16(str) {<br> var out, i, len, c;<br> var char2, char3;</p> <p> out = "";<br> len = str.length;<br> i = 0;<br> while(i < len) {<BR> c = str.charCodeAt(i++);<BR> switch(c >> 4)<br> { <br> case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:<br> // 0xxxxxxx<br> out += str.charAt(i-1);<br> break;<br> case 12: case 13:<br> // 110x xxxx 10xx xxxx<br> char2 = str.charCodeAt(i++);<br> out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));<br> break;<br> case 14:<br> // 1110 xxxx 10xx xxxx 10xx xxxx<br> char2 = str.charCodeAt(i++);<br> char3 = str.charCodeAt(i++);<br> out += String.fromCharCode(((c & 0x0F) << 12) |<br> ((char2 & 0x3F) << 6) |<br> ((char3 & 0x3F) << 0));<br> break;<br> }<br> }</p> <p> return out;<br>}</p> <p>/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp><br>* Version: 1.0<br>* LastModified: Dec 25 1999<br>* This library is free. You can redistribute it and/or modify it.<br>*/</p> <p>/*<br>* Interfaces:<br>* b64 = base64encode(data);<br>* data = base64decode(b64);<br>*/</p> <p><br>var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";<br>var base64DecodeChars = new Array(<br> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,<br> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,<br> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,<br> 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,<br> -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,<br> 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,<br> -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,<br> 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);</p> <p>function base64encode(str) {<br> var out, i, len;<br> var c1, c2, c3;</p> <p> len = str.length;<br> i = 0;<br> out = "";<br> while(i < len) {<BR> c1 = str.charCodeAt(i++) & 0xff;<BR> if(i == len)<BR> {<BR> out += base64EncodeChars.charAt(c1 >> 2);<br> out += base64EncodeChars.charAt((c1 & 0x3) << 4);<BR> out += "==";<BR> break;<BR> }<BR> c2 = str.charCodeAt(i++);<BR> if(i == len)<BR> {<BR> out += base64EncodeChars.charAt(c1 >> 2);<br> out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));<br> out += base64EncodeChars.charAt((c2 & 0xF) << 2);<BR> out += "=";<BR> break;<BR> }<BR> c3 = str.charCodeAt(i++);<BR> out += base64EncodeChars.charAt(c1 >> 2);<br> out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));<br> out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));<br> out += base64EncodeChars.charAt(c3 & 0x3F);<br> }<br> return out;<br>}</p> <p>function base64decode(str) {<br> var c1, c2, c3, c4;<br> var i, len, out;</p> <p> len = str.length;<br> i = 0;<br> out = "";<br> while(i < len) {<br> /* c1 */<br> do {<br> c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];<br> } while(i < len && c1 == -1);<br> if(c1 == -1)<br> break;</p> <p> /* c2 */<br> do {<br> } while(i if(c2 == -1)<br> 與中斷使用;<br> </p> out = String.fromCharCode((c1 > 4));<p> </p> /* c3 */<p> do {<br> if(c3 == 61)<br> return出;<br> return c3];<br> } while(i <br> out = String.fromCharCode(((c2 & 0XF) > 2));<br> <br> /* c4 */</p> do {<p> if(c4 == 61)</p> return出;<p> return c4];<br> } while(i out = String.fromCharCode(((c3 & 0x03 ) ) }<br> return out;<br>}<br>//輸入base64編碼}<br><br><br></p> </div>