Languages
[Edit]
EN

TypeScript / Node.js - Base64 encode and decode

3 points
Created by:
JoanneSenior
1070

In this article, we would like to show you how to encode and decode Base64 in TypeScript. under Node.js.

Quick solution:

const text: string = 'some text...';

const encoded: string = Buffer.from(text, 'utf8').toString('base64');
const decoded: string = Buffer.from(encoded, 'base64').toString('utf8');

console.log('Encoded text: ' + encoded);  // c29tZSB0ZXh0Li4u
console.log('Decoded text: ' + decoded);  // some text...

 

Encoding

In this example, we create a Base64-encoded ASCII string using Buffer.from() with toString() method.

const text: string = 'some text...';
const encoded: string = Buffer.from(text, 'utf8').toString('base64');

console.log('Encoded text: ' + encoded); // c29tZSB0ZXh0Li4u

Output:

Encoded text: c29tZSB0ZXh0Li4u

 

Decoding

In this example, we decode a string of data that has been encoded using Base64 encoding.

const encoded: string = 'c29tZSB0ZXh0Li4u';
const decoded: string = Buffer.from(encoded, 'base64').toString('utf8');

console.log('Decoded text: ' + decoded); // some text...

Output:

Decoded text: some text...

 

Alternative titles

  1. TypeScript - Base 64 encode and decode
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join