Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs string to base64

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World
Comment

nodejs base64

//--------------- HOW TO ENCODE BASE64 ON NODEJS ----------------------
//create a buffer of the text "Hello World"
var buffer = Buffer.from('Hello World');
//buffer result is: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>

var string64 = buffer.toString('base64');
// .toString('base64') allow to encode it to base64
// result is: SGVsbG8gV29ybGQ=

// Can be use combined together like these
console.log(Buffer.from('Hello World').toString('base64'));
// result is: SGVsbG8gV29ybGQ=
Comment

nodejs base64

//--------------- HOW TO DECODE BASE64 ON NODEJS ----------------------
//create a buffer of the text "Hello World"
var buffer = Buffer.from('SGVsbG8gV29ybGQ=', 'base64');
//buffer result is: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>

var string64 = buffer.toString('ascii');
// .toString('ascii') allow to decode base64
// result is: "Hello World"

// Can be use combined together like these
console.log(Buffer.from('SGVsbG8gV29ybGQ=', 'base64').toString('ascii'));
// result is: "Hello World"
Comment

NODEJS ES6 STRING TO BASE64

Encoding a string to base64
const buff = Buffer.from("hi readers", "utf8");
const base64 = buff.toString("base64");
console.log(base64); // aGkgcmVhZGVycw==

Decoding a base64 to a string
Similarly, we can decode a base64 encoding to a string like this.
const buff = Buffer.from("aGkgcmVhZGVycw==", "base64");
const str = buff.toString("utf8");
console.log(str); // hi readers
Comment

Encoding and Decoding Base64 Strings in Node.js

'use strict';

let data = 'stackabuse.com';
let buff = new Buffer(data);
let base64data = buff.toString('base64');

console.log('"' + data + '" converted to Base64 is "' + base64data + '"');
Comment

PREVIOUS NEXT
Code Example
Javascript :: bootstrap show modal on page load 
Javascript :: check comma in string javascript 
Javascript :: javascript repeat each second 
Javascript :: nextjs api example typescript 
Javascript :: mouseover angular 6 
Javascript :: grafana labs node exporter 
Javascript :: socket io with cors 
Javascript :: js set class 
Javascript :: javascript int max 
Javascript :: open link in new tab jquery 
Javascript :: node load string from file 
Javascript :: moment remove one day 
Javascript :: console.log big red text 
Javascript :: eslint change max line length 
Javascript :: How to tell if an attribute exists on an object 
Javascript :: javascript split get last element 
Javascript :: english number to bangla javascript 
Javascript :: react fetch post 
Javascript :: stop immediate propagation 
Javascript :: parsefloat jquery 
Javascript :: ReactDOM.render is no longer supported in React 18. Use createRoot instead 
Javascript :: js create div 
Javascript :: check if a variable is array in javascript 
Javascript :: how to delay iterations in javascript 
Javascript :: unable to resolve path to module eslint 
Javascript :: node js pre-commit hook bypass 
Javascript :: how can i validate an email address in javascript 
Javascript :: get search params from url react 
Javascript :: jquery detect if element has overflow 
Javascript :: jquery click on parent but not child 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =