1var string = "Hello folks how are you doing today?";
2var encodedString = btoa(string); // Base64 encode the String
3var decodedString = atob(encodedString); // Base64 decode the String
4
1// base64 to string
2let base64ToString = Buffer.from(obj, "base64").toString();
3base64ToString = JSON.parse(base64ToString);
4
5//or
6let str = 'bmltZXNoZGV1amEuY29t';
7let buff = new Buffer(str, 'base64');
8let base64ToStringNew = buff.toString('ascii');
9
10// string to base64
11let data = 'nimeshdeuja.com';
12
13var string = "Hello folks how are you doing today?";
14var encodedString = btoa(string); // Base64 encode the String
15var decodedString = atob(encodedString); // Base64 decode the String
1// base64 to string
2let base64ToString = Buffer.from(obj, "base64").toString();
3base64ToString = JSON.parse(base64ToString);
4
5//or
6let str = 'bmltZXNoZGV1amEuY29t';
7let buff = new Buffer(str, 'base64');
8let base64ToStringNew = buff.toString('ascii');
9
10// string to base64
11let data = 'nimeshdeuja.com';
12let buff = new Buffer(data);
13let stringToBase64 = buff.toString('base64');
1// Define the string
2var string = 'Hello World!';
3
4// Encode the String
5var encodedString = btoa(string);
6console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
7
8// Decode the String
9var decodedString = atob(encodedString);
10console.log(decodedString); // Outputs: "Hello World!"
11
1var decode = function(input) {
2 // Replace non-url compatible chars with base64 standard chars
3 input = input
4 .replace(/-/g, '+')
5 .replace(/_/g, '/');
6
7 // Pad out with standard base64 required padding characters
8 var pad = input.length % 4;
9 if(pad) {
10 if(pad === 1) {
11 throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
12 }
13 input += new Array(5-pad).join('=');
14 }
15
16 return input;
17 }
18