最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Encoding and decoding text with special characters (àâäèéêë)

programmeradmin8浏览0评论

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
  • 2 You should convert the original string to UTF8 in the first place, to get the bytes that are generated by the UTF8 encoding. As it is, in this case only, you can probably use Encoding.Latin1.GetString(data) (assuming you're targeting .NET5+) – Jimi Commented Mar 19 at 4:28
  • See base64.guru/developers/javascript/examples/unicode-strings and stackoverflow/questions/30106476/… – Martheen Commented Mar 19 at 4:31
  • let encodedText = window.btoa(unescape(encodeURIComponent("demoé"))); // ZGVtb8Op – Sani Huttunen Commented Mar 19 at 4:36
  • 3 window.atob() and window.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
  • 1 FYI the Latin1 workaround suggested by Jimi works because the first 256 codepoints in Unicode are Latin1, so for average diacritics you'll find, it works just fine. Things broke once you use Arabics, CJK, Hangul, Hebrew, Thai etc – Martheen Commented Mar 19 at 4:48
Add a comment  | 

1 Answer 1

Reset to default 0

encode 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é"
发布评论

评论列表(0)

  1. 暂无评论