Languages
[Edit]
EN

TypeScript - atob / btoa functions equivalents in Node.js

3 points
Created by:
Bess
601

In this article, we would like to show you atob and btoa functions equivalents in TypeSrcipt in Node.js.

Note:

In the presented solution, we use the Buffer class, which is in a global scope in Node.js and does not need to be imported using the require() function.

The only thing you need to do is to install Node.js types for development using:

npm i --save-dev @types/node

btoa function equivalent

In web browser JavaScript btoa() function is used to convert binary to Base64-encoded ASCII string. btoa function name should be read as "binary to ASCII".

const toBase64 = (text: string): string => {
  return Buffer.from(text, 'binary').toString('base64');
};

console.log(toBase64('This is text...')); // VGhpcyBpcyB0ZXh0Li4u

Output:

VGhpcyBpcyB0ZXh0Li4u

atob function equivalent

In web browser JavaScript atob() function is used to convert Base64-encoded ASCII string to binary. atob function name should be read as "ASCII to binary".

const toBinary = (base64: string): string => {
  return Buffer.from(base64, 'base64').toString('binary');
};

console.log(toBinary('VGhpcyBpcyB0ZXh0Li4u')); // This is text...

Output:

This is text...
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