目录

chen 的个人博客

VX:TiAmo151028
Phone:13403656751
Email:zxydczzs@gmail.com

JavaScript将字符串base64编码

 1function base64Encode(baseSting) {
 2	var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 3	var length = baseSting.length;
 4	var chara1, chara2, chara3;
 5	var out = "",
 6		i = 0;
 7	while (i < length) {
 8		chara1 = baseSting.charCodeAt(i++) & 0xff;
 9		out += base64Chars.charAt(chara1 >> 2);
10		if (i == length) {
11			out += base64Chars.charAt((chara1 & 0x3) << 4);
12			out += "==";
13			break;
14		}
15		chara2 = baseSting.charCodeAt(i++);
16		if (i == length) {
17			out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4));
18			out += base64Chars.charAt((chara2 & 0xF) << 2);
19			out += "=";
20			break;
21		}
22		chara3 = baseSting.charCodeAt(i++);
23		out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4));
24		out += base64Chars.charAt(((chara2 & 0xF) << 2) | ((chara3 & 0xC0) >> 6));
25		out += base64Chars.charAt(chara3 & 0x3F);
26	}
27	return out;
28}

标题:JavaScript将字符串base64编码
作者:zzzzchen
地址:https://dczzs.com/articles/2021/05/31/1622441215112.html