目录

zzzzchen 的个人博客

记录精彩的程序人生

JavaScript将字符串base64编码

function base64Encode(baseSting) {
	var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var length = baseSting.length;
	var chara1, chara2, chara3;
	var out = "",
		i = 0;
	while (i < length) {
		chara1 = baseSting.charCodeAt(i++) & 0xff;
		out += base64Chars.charAt(chara1 >> 2);
		if (i == length) {
			out += base64Chars.charAt((chara1 & 0x3) << 4);
			out += "==";
			break;
		}
		chara2 = baseSting.charCodeAt(i++);
		if (i == length) {
			out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4));
			out += base64Chars.charAt((chara2 & 0xF) << 2);
			out += "=";
			break;
		}
		chara3 = baseSting.charCodeAt(i++);
		out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4));
		out += base64Chars.charAt(((chara2 & 0xF) << 2) | ((chara3 & 0xC0) >> 6));
		out += base64Chars.charAt(chara3 & 0x3F);
	}
	return out;
}

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