2

How can I decode UTF8 bytes in a string in C#?

Example: Decode this input:

"Poluci%C3%B3n"

To output this:

"Polución"
5
  • Do you have the source array? You could try System.Text.Encoding.UTF8.GetString(byteArray). Commented Aug 6, 2012 at 14:50
  • 7
    That's not UTF-8 encoded - it is URL encoded (aka percentage encoded). Commented Aug 6, 2012 at 14:50
  • What you have posted are UTF-16 strings, since we're talking about C#. What you want to do is urldecode... Commented Aug 6, 2012 at 14:52
  • @oded - This didn't come from a URL, although it is similar. I believe c3b3 is the hex representation in UTF8 for the U+00F3 unicode code point (ó). Commented Aug 6, 2012 at 15:05
  • @seldary - That would make it a URL encoded UTF-8 string. Commented Aug 6, 2012 at 15:07

2 Answers 2

9

This encoding appears to be URL encoding (not UTF-8 encoding). You can unencode it with a number of different methods in .NET:

HttpUtility.UrlDecode("Poluci%C3%B3n"); // returns "Polución"
Uri.UnescapeDataString("Poluci%C3%B3n"); // returns "Polución"
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

Uri.UnescapeDataString("Poluci%C3%B3n")

the problem has nothing to do with UTF8 though. It's just URL encoded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.