Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Encoding conversion?

I have a string which looks like:

 À l'intérieur

But it needs to be:

 À l'intérieur

I tried changing the conversion. I know it's read as ASCII encoding. So I tried using the code:

      ASCIIEncoding ASCII  = new System.Text.ASCIIEncoding();
      Byte[] BytesMessage = ASCII.GetBytes(Title);
      Title = Encoding.UTF8.GetString(BytesMessage);

I tried switching between the different encodings but it didn't help much. Is there a way to fix this?

Thx!

like image 447
WtFudgE Avatar asked Nov 21 '25 08:11

WtFudgE


2 Answers

Well, this is actually not a character encoding (such as ASCII or UTF-8) but rather a higher-level protocol defining escape sequences for arbitrary characters—in this case SGML which serves as the basis for HTML and XML.

You can use the HtmlDecode method of the HttpUtility class to decode it:

PS> Add-Type -AssemblyName system.web
PS> [web.httputility]::HtmlDecode("À l'intérieur")
À l'intérieur

However, this class resides in the System.Web namespace so it's probably not immediately available in a non-ASP.NET project.

like image 92
Joey Avatar answered Nov 23 '25 23:11

Joey


To transcode HTML encoded strings like this use System.Web.HttpUtility.HtmlDecode(Title). You'll need to reference System.Web if this is not a Web application.

like image 41
Jeremy McGee Avatar answered Nov 23 '25 23:11

Jeremy McGee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!