views:

154

answers:

2

Hi,

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

+5  A: 

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

HttpUtility.UrlEncode(value)
David M
Thanks, but in my example this method returns: "This+(is)+my+string"It doesn't seem to replace the ( and the ) characters.
JP
Why would you want to? On what are you basing your decision as to what characters get escaped and what don't?
David M
That's how it is specified by my client, see my comment below.
JP
HTML form encoding, accordingly to the HTML specification, is URL encoding. It refers to RFC 1738 (http://www.ietf.org/rfc/rfc1738.txt), which states that ( and ) are not reserved characters and therefore don't need to be encoded.
David M
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?
JP
They have probably hand-cranked some sort of encoding that replaces spaces with + and then replaces all non-alphanumeric characters with %+hex code.
David M
+1  A: 

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.)

Jeremy McGee
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?
JP