0

How can easily encode a string to utf8 using .NET (VB or C#)? For instance I need to encode a string like "This (is) my string" the result should be a string "This+%28is%29+my+string".

TIA

JP

1
  • 4
    That is not utf-8 encoding. You mean urlencoding. Commented Dec 11, 2009 at 13:29

2 Answers 2

7

This is URL encoding, not UTF8 encoding. Try this method:

HttpUtility.UrlEncode(value)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but in my example this method returns: "This+(is)+my+string" It doesn't seem to replace the ( and the ) characters.
Why would you want to? On what are you basing your decision as to what characters get escaped and what don't?
That's how it is specified by my client, see my comment below.
HTML form encoding, accordingly to the HTML specification, is URL encoding. It refers to RFC 1738 (ietf.org/rfc/rfc1738.txt), which states that ( and ) are not reserved characters and therefore don't need to be encoded.
Thank you very much, I wil report this back to my client. Do you have any idea as to how they will have implemented this, using another encoding standard perhaps? Or do think think it is something they have invented themself?
|
1

It looks like what you need to do is URL encoding, not "encoding to UTF-8".

For that, use

string encodedString = System.Web.HttpUtility.UrlEncode(yourString)

(UTF-8 is a mechanism for representing Unicode characters internally, not for replacing characters that otherwise have meaning when used in a URI.)

1 Comment

It doesn't seem to replace the ( and the ) characters. I found some more information about the encoding I should be using from my client. It supposed to be HTML form encoding, according the UTF-8 transformation format. How do I do that?

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.