xxxxxxxxxx
const stringToBase64 = (str) => {
const base64 = btoa(str);
return base64;
};
const inputString = "Hello World!";
const base64String = stringToBase64(inputString);
console.log(base64String);
xxxxxxxxxx
> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World
xxxxxxxxxx
var string = "Hello folks how are you doing today?";
var encodedString = btoa(string); // Base64 encode the String
var decodedString = atob(encodedString); // Base64 decode the String
xxxxxxxxxx
// base64 to string
let base64ToString = Buffer.from(obj, "base64").toString();
base64ToString = JSON.parse(base64ToString);
//or
let str = 'bmltZXNoZGV1amEuY29t';
let buff = new Buffer(str, 'base64');
let base64ToStringNew = buff.toString('ascii');
// string to base64
let data = 'nimeshdeuja.com';
let buff = new Buffer(data);
let stringToBase64 = buff.toString('base64');
xxxxxxxxxx
let str = 'bmltZXNoZGV1amEuY29t';
let buff = new Buffer(str, 'base64');
let base64ToStringNew = buff.toString('utf8');
xxxxxxxxxx
const data = 'CodezUp';
console.log('---ORIGINAL-----', data)
// Encode String
const encode = Buffer.from(data).toString('base64')
console.log('\n---ENCODED-----', encode)
// Decode String
const decode = Buffer.from(encode, 'base64').toString('utf-8')
console.log('\n---DECODED-----', decode)
xxxxxxxxxx
const base64Encode = (string) => {
const newText = btoa(string);
return newText;
};
xxxxxxxxxx
const encoded = window.btoa('Alireza Dezfoolian'); // encode a string
const decoded = window.atob(encoded); // decode the string
xxxxxxxxxx
str = "The quick brown fox jumps over the lazy dog";
b64 = btoa(unescape(encodeURIComponent(str)));
str = decodeURIComponent(escape(window.atob(b64)));