Newer
Older
export const stringToUtf8ByteArray = (str) => {
if (typeof str !== 'string') {
str = str.toString();
}
const res = Buffer.from(str, 'utf-8');
return res;
}
export const utf8ByteArrayToString = (ba) => {
if (!Buffer.isBuffer(ba)) {
ba = Buffer.from(ba);
}
const res = ba.toString('utf-8');
return res;
}
export const stringToUtf8Base64 = (str) => {
if (!Buffer.isBuffer(str)) {
if (typeof str !== 'string') {
str = str.toString();
}
str = Buffer.from(str, 'utf-8');
}
const res = str.toString('base64');
return res;
}
export const utf8Base64ToString = (strBase64) => {
if (!Buffer.isBuffer(strBase64)) {
if (typeof strBase64 !== 'string') {
strBase64 = strBase64.toString();
}
strBase64 = Buffer.from(strBase64, 'base64');
}
const res = strBase64.toString('utf-8');
return res;
}
export const base64ToByteArray = (strBase64) => {
if (typeof strBase64 !== 'string') {
strBase64 = strBase64.toString();
}
const res = Buffer.from(strBase64, 'base64');
return res;
}
export const byteArrayToBase64 = (ba) => {
if (!Buffer.isBuffer(ba)) {
ba = Buffer.from(ba);
}
const res = ba.toString('base64');
return res;
}