I am trying to encode the text in Angular :
let encodedText = window.btoa("demoé"); //ZGVtb+k=
When trying to decode the same text in C#, the conversion is not happening properly:
string encodedText= "ZGVtb+k=";
byte[] data = Convert.FromBase64String(encodedText);
string decodedString =Encoding.UTF8.GetString(data); //demo�
After decoding, I am getting the value as "demo�".
Kindly suggest if there is any other approach to handle this scenario.
Thanks in advance
I am trying to encode the text in Angular :
let encodedText = window.btoa("demoé"); //ZGVtb+k=
When trying to decode the same text in C#, the conversion is not happening properly:
string encodedText= "ZGVtb+k=";
byte[] data = Convert.FromBase64String(encodedText);
string decodedString =Encoding.UTF8.GetString(data); //demo�
After decoding, I am getting the value as "demo�".
Kindly suggest if there is any other approach to handle this scenario.
Thanks in advance
Share Improve this question edited Mar 19 at 5:11 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 19 at 4:12 chaitanya reddychaitanya reddy 71 silver badge1 bronze badge 5 |1 Answer
Reset to default 0encode in angular:
let text = "demoé";
let encodedText = btoa(unescape(encodeURIComponent(text)));
The encodeURIComponent() method encodes special characters ...
The unescape() function computes a new string in which hexadecimal escape sequences are replaced with the characters that they represent.
decode in c#:
string encodedText = "ZGVtb8Op";
byte[] data = Convert.FromBase64String(encodedText);
string decodedText = Encoding.UTF8.GetString(data);
//"demoé"
Encoding.Latin1.GetString(data)
(assuming you're targeting .NET5+) – Jimi Commented Mar 19 at 4:28let encodedText = window.btoa(unescape(encodeURIComponent("demoé"))); // ZGVtb8Op
– Sani Huttunen Commented Mar 19 at 4:36window.atob()
andwindow.btoa()
operate on byte data, it will not work with multibyte encoded strings. You need to convert it to bytes first prior to encoding on the client side. ref – Jeff Mercado Commented Mar 19 at 4:42