// 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');
let str = 'bmltZXNoZGV1amEuY29t';
let buff = new Buffer(str, 'base64');
let base64ToStringNew = buff.toString('utf8');
// Define the string
var string = 'Hello World!';
// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
const data = 'CodezUp';
console.log('---ORIGINAL-----', data)
// Encode String
const encode = Buffer.from(data).toString('base64')
console.log('
---ENCODED-----', encode)
// Decode String
const decode = Buffer.from(encode, 'base64').toString('utf-8')
console.log('
---DECODED-----', decode)
var decodedString = atob(encodedString);
const base64Encode = (string) => {
const newText = btoa(string);
return newText;
};
/**
* Function that converts a base64 string into a base64url string
* @param {string} input - The string to convert
*/
function base64ToBase64url(input) {
// Replace non-url compatible chars with base64url standard chars and remove leading =
return input
.replace(/+/g, '_')
.replace(///g, '-')
.replace(/=+$/g, '');
}