如何将node.js中的位串转成base64?
How to convert a bit string to base64 in node.js?
我有一个要转换为 base64 的位字符串,但看起来没有执行此操作的本机函数,而且我也找不到节点模块。 ):
输入:100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011
输出:等效二进制值的 base64 表示
也许更好的问题是如何将位串转换成缓冲区?不确定
如您所料,主要是将字符串转换为更容易转换为 base64 的内容,然后再将其转换为 base64。
在下面的代码中,我们执行这些转换序列:
- 位字符串 -> BigInt -> 字节大小的整数数组 -> 二进制字符串 -> base64
- base64 -> 二进制字符串 -> 字节大小的位串数组 -> 位串
const encode = bitstr => {
const bytes = [];
// convert bit string to BigInt
let value = BigInt('0b' + bitstr);
// chop it up into bytes
while (value > 0n) {
bytes.unshift(Number(value & 0xffn));
value >>= 8n;
}
// convert to binary string and encode as base64
return btoa(String.fromCharCode.apply(null, bytes));
};
const decode = b64 => {
// decode base64 to binary string
const bstr = atob(b64);
// convert binary string to bit string
return new Array(bstr.length).fill(0).map(
(_,i) => bstr.charCodeAt(i).toString(2).padStart(8, i ? '0' : '')
).join('');
};
const bitstr = '100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011';
const encoded = encode(bitstr);
const decoded = decode(encoded);
console.log(bitstr);
console.log(encoded);
console.log(decoded);
我有一个要转换为 base64 的位字符串,但看起来没有执行此操作的本机函数,而且我也找不到节点模块。 ):
输入:100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011
输出:等效二进制值的 base64 表示
也许更好的问题是如何将位串转换成缓冲区?不确定
如您所料,主要是将字符串转换为更容易转换为 base64 的内容,然后再将其转换为 base64。
在下面的代码中,我们执行这些转换序列:
- 位字符串 -> BigInt -> 字节大小的整数数组 -> 二进制字符串 -> base64
- base64 -> 二进制字符串 -> 字节大小的位串数组 -> 位串
const encode = bitstr => {
const bytes = [];
// convert bit string to BigInt
let value = BigInt('0b' + bitstr);
// chop it up into bytes
while (value > 0n) {
bytes.unshift(Number(value & 0xffn));
value >>= 8n;
}
// convert to binary string and encode as base64
return btoa(String.fromCharCode.apply(null, bytes));
};
const decode = b64 => {
// decode base64 to binary string
const bstr = atob(b64);
// convert binary string to bit string
return new Array(bstr.length).fill(0).map(
(_,i) => bstr.charCodeAt(i).toString(2).padStart(8, i ? '0' : '')
).join('');
};
const bitstr = '100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011';
const encoded = encode(bitstr);
const decoded = decode(encoded);
console.log(bitstr);
console.log(encoded);
console.log(decoded);