Skip to content

Instantly share code, notes, and snippets.

@zhuzhuaicoding
Created September 10, 2013 05:21
Show Gist options
  • Select an option

  • Save zhuzhuaicoding/6505290 to your computer and use it in GitHub Desktop.

Select an option

Save zhuzhuaicoding/6505290 to your computer and use it in GitHub Desktop.
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", base64DecodeChars = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];
/**
* @description base64转码
* @author jeking、classyuan
* @param {String} d 转码字符串
* @return {String}
* @example
base64encode("nihao");//bmloYW8=
*/
function base64encode(d) {
var a, b, e, c, f, g;
e = d.length;
b = 0;
for (a = ""; b < e;) {
c = d.charCodeAt(b++) & 255;
if (b == e) {
a += base64EncodeChars.charAt(c >> 2);
a += base64EncodeChars.charAt((c & 3) << 4);
a += "==";
break
}
f = d.charCodeAt(b++);
if (b == e) {
a += base64EncodeChars.charAt(c >> 2);
a += base64EncodeChars.charAt((c & 3) << 4 | (f & 240) >> 4);
a += base64EncodeChars.charAt((f & 15) << 2);
a += "=";
break
}
g = d.charCodeAt(b++);
a += base64EncodeChars.charAt(c >> 2);
a += base64EncodeChars.charAt((c & 3) << 4 | (f & 240) >> 4);
a += base64EncodeChars.charAt((f & 15) << 2 | (g & 192) >>
6);
a += base64EncodeChars.charAt(g & 63)
}
return a
}
/**
* @description base64解码
* @author jeking、classyuan
* @param {String} d 转码字符串
* @return {String}
* @example
base64encode("bmloYW8=");//nihao
*/
function base64decode(d) {
var a, b, e, c, f;
c = d.length;
e = 0;
for (f = ""; e < c;) {
do a = base64DecodeChars[d.charCodeAt(e++) & 255]; while (e < c && -1 == a);
if (-1 == a)break;
do b = base64DecodeChars[d.charCodeAt(e++) & 255]; while (e < c && -1 == b);
if (-1 == b)break;
f += String.fromCharCode(a << 2 | (b & 48) >> 4);
do {
a = d.charCodeAt(e++) & 255;
if (61 == a)return f;
a = base64DecodeChars[a]
} while (e < c && -1 == a);
if (-1 == a)break;
f += String.fromCharCode((b & 15) << 4 | (a & 60) >> 2);
do {
b = d.charCodeAt(e++) & 255;
if (61 == b)return f;
b = base64DecodeChars[b]
} while (e < c && -1 == b);
if (-1 ==
b)break;
f += String.fromCharCode((a & 3) << 6 | b)
}
return f
}
function utf16to8(d) {
var a, b, e, c;
a = "";
e = d.length;
for (b = 0; b < e; b++)c = d.charCodeAt(b), 1 <= c && 127 >= c ? a += d.charAt(b) : (2047 < c ? (a += String.fromCharCode(224 | c >> 12 & 15), a += String.fromCharCode(128 | c >> 6 & 63)) : a += String.fromCharCode(192 | c >> 6 & 31), a += String.fromCharCode(128 | c >> 0 & 63));
return a
}
function utf8to16(d) {
var a, b, e, c, f, g;
a = "";
e = d.length;
for (b = 0; b < e;)switch (c = d.charCodeAt(b++), c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
a += d.charAt(b - 1);
break;
case 12:
case 13:
f = d.charCodeAt(b++);
a += String.fromCharCode((c & 31) << 6 | f & 63);
break;
case 14:
f = d.charCodeAt(b++), g = d.charCodeAt(b++), a += String.fromCharCode((c & 15) << 12 | (f & 63) << 6 | (g & 63) << 0)
}
return a
};
@zhuzhuaicoding
Copy link
Copy Markdown
Author

utfEncode: function (a) {
for (var a = a.replace(/\r\n/g, "\n"), b = "", c = 0, g = a.length; c < g; c++) {
var i = a.charCodeAt(c);
128 > i ? b += String.fromCharCode(i) : (127 < i && 2048 > i ? b += String.fromCharCode(i >> 6 | 192) : (b += String.fromCharCode(i >> 12 | 224), b += String.fromCharCode(i >> 6 & 63 | 128)), b += String.fromCharCode(i & 63 | 128))
}
return b
}, utfDecode: function (a) {
for (var b = "", c = 0, g = c1 = c2 = c3 = 0; c < a.length;)g = a.charCodeAt(c), 128 > g ? (b += String.fromCharCode(g), c++) : 191 < g && 224 > g ? (c2 = a.charCodeAt(c + 1), b += String.fromCharCode((g & 31) << 6 | c2 & 63), c += 2) : (c2 = a.charCodeAt(c + 1), c3 = a.charCodeAt(c + 2), b += String.fromCharCode((g & 15) << 12 | (c2 & 63) << 6 | c3 & 63), c += 3);
return b
},

@zhuzhuaicoding
Copy link
Copy Markdown
Author

t.qq.com mi_.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment