/**
* 字符串转base64
* @param str
* @returns
*/
function myEncode(str) {
// // 对字符串进行编码
var encode = encodeURI(str.replace(/\+/g, '躞'));//+在后台转明文会丢
// // 对编码的字符串转化base64
var base64 = btoa(encode);
return base64;
}
/**
* 字符串转二进制
*/
function stringToBinary(str) {
let binary = '';
var encode = encodeURI(str.replace(/\+/g, '躞'));//对字符串进行编码,不转中文有问题(+在后台转明文会丢)
for (let i = 0; i < encode.length; i++) {
let charCode = encode.charCodeAt(i);
let binaryCode = charCode.toString(2);
binary += binaryCode.padStart(8, '0');
}
return binary;
}
标签:binary,二进制,base64,encode,let,str,字符串
From: https://www.cnblogs.com/anjun-xy/p/18091256