How to encode or decode a string in angular 2 with base64 ??? My front-end tool is Angular 2. I had a password string, before passing it to API I need to base64 encode. Since in service base64 encoded string will be decoded.
So I am looking for some base64 encode/decode library for Angular2/Typescript and some options.
Thanks!!!
To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.
Base64 Encoding means of encoding text in ASCII(American Standard Code for Information Interchange) format. The Base64 scheme takes any kind of data type, such as binary data, and translates it into text format in ASCII. If you want to Encode, the method name is btoa().
The btoa() method encodes a string in base-64. The btoa() method uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode the string.
Use the btoa()
function to encode:
console.log(btoa("password")); // cGFzc3dvcmQ=
To decode, you can use the atob()
function:
console.log(atob("cGFzc3dvcmQ=")); // password
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With