How do I encode and decode a string with base64 in Angular using typescript?

1 minute read

You can encode the string to ASCII and decode it back to the string in Angular using Typescript. Let’s find out how to perform the encoding and decoding of the base64 string.

What is btoa? And stand for?

btoa() method: Binary to ASCII

This method creates a base64 encoded ASCII string from a binary string.

What is atob? And stand for?

atob() method: ASCII to Binary

This mehod decodes a string encoded using base64.

How to use btoa and atob in Angular using Typescript?

Example:

1. Use the btoa() method to encode the string where you want the encoding.
    var encodedString = btoa("Hello")
    It returns the encoded data and stores it in the encodedString variable.

2. Use the atob() method to decode the encoded string. 
    var decodedString = btoa(encodedString)
    It decodes the base64 encoded string and stores in the decodedString variable.

Leave a comment