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!!!
Use the btoa()
function to encode:
console.log(btoa("password")); // cGFzc3dvcmQ=
To decode, you can use the atob()
function:
console.log(atob("cGFzc3dvcmQ=")); // password
Use btoa("yourstring")
more info: https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
TypeScript is a superset of Javascript, it can use existing Javascript libraries and web APIs
Use btoa()
for encode and atob()
for decode
text_val:any="your encoding text";
Encoded Text: console.log(btoa(this.text_val)); //eW91ciBlbmNvZGluZyB0ZXh0
Decoded Text: console.log(atob("eW91ciBlbmNvZGluZyB0ZXh0")); //your encoding text
For encoding to base64 in Angular2, you can use btoa() function.
Example:-
console.log(btoa("stringAngular2"));
// Output:- c3RyaW5nQW5ndWxhcjI=
For deconding from base64 in Angular2, you can use atob() function.
Example:-
console.log(atob("c3RyaW5nQW5ndWxhcjI="));
// Output:- stringAngular2