Change to character code

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the an url, for example:
"http://domain.com/test.pdf"

And I need to generate a string as follows:
"http%3A%2F%2Fdomain.com%2Ftest.pdf"

Basically I am replacing /, :, , etc in the url by its code. How can I
do this?

Thanks,
Miguel
 
shapper said:
Hello,

I have the an url, for example:
"http://domain.com/test.pdf"

And I need to generate a string as follows:
"http%3A%2F%2Fdomain.com%2Ftest.pdf"

Basically I am replacing /, :, , etc in the url by its code. How can I
do this?

What is your specific need here? Can you provide a precise
specification for the behavior you want?

The Uri.EscapeUriString() method should do what you want, at least given
the example you've provided.

If that method doesn't meet your needs, it would be helpful if you could
provide a completely unambiguous description of the processing you want
to do. Hint: such a description won't include the word "etcetera". :)

Pete
 
shapper said:
Hello,

I have the an url, for example:
"http://domain.com/test.pdf"

And I need to generate a string as follows:
"http%3A%2F%2Fdomain.com%2Ftest.pdf"

Basically I am replacing /, :, , etc in the url by its code. How can I
do this?

Something like this:

string Encode(string input)
{
StringBuilder result = new StringBuilder();
foreach (byte b in Encoding.UTF8.GetBytes(input))
{
if (b == 0x2E /* . */ || (b >= 0x30 && b <= 0x39) /* 0-9 */ || (b >=
0x41 && b <= 0x5a) /* A-Z */ || (b >= 0x61 && b <= 0x7a) /* a-z */)
result.Append((char)b);
else
result.Append("%" + b.ToString("X2"));
}
}

Good for protecting SQL queries, filenames, all sorts of things against
special characters. Always use a whitelist of what's ok to leave alone,
instead of a blacklist of things to replace. Always.
 
Peter said:
What is your specific need here? Can you provide a precise
specification for the behavior you want?

The Uri.EscapeUriString() method should do what you want, at least given
the example you've provided.

If that method doesn't meet your needs, it would be helpful if you could
provide a completely unambiguous description of the processing you want
to do. Hint: such a description won't include the word "etcetera". :)

Pete

Unless I'm missing something, Uri.EscapeUriString() would leave the
string "http://domain.com/test.pdf" unchanged.
 
shapper said:
I have the an url, for example:
"http://domain.com/test.pdf"

And I need to generate a string as follows:
"http%3A%2F%2Fdomain.com%2Ftest.pdf"

Basically I am replacing /, :, , etc in the url by its code. How can I
do this?

s = HttpUtility.UrlEncode(s);

(it is in System.Web)

Arne
 
Family said:
Unless I'm missing something, Uri.EscapeUriString() would leave the
string "http://domain.com/test.pdf" unchanged.

Then the documentation is wrong. It reads "By default, the
EscapeUriString method converts all characters, except RFC 2396
unreserved characters, to their hexadecimal representation".

In turn, RFC 2396 describes "unreserved characters" as "upper and lower
case letters, decimal digits, and a limited set of punctuation marks and
symbols". The "limited set of punctuation marks and symbols" are: "-",
"_", ".", "!", "~", "*", "'", "(", and ")".

Notably absent in that collection of punctuation marks and symbols are
the colon ":" and the forward slash "/".

I was relying on the documentation before. After your reply, I went
ahead and tested it, and sure enough the colon and forward slash are
also left unchanged (there are many other reserved characters also left
unchanged, as it happens...of a short sample of characters, I found only
"%" and "^" getting escaped).

The documentation claims that RFC 2396 behavior is the default, and is
only changed if the application configuration is modified so that
IriParsing is "true". However, in my test not only did I not set that
setting to "true", I tried explicitly setting it to false, and I still
see the method failing to escape those characters.

Fortunately, the alternative method that Arne suggests appears to work
just fine.

Time to go submit yet another bug report on the documentation...

Pete
 
Back
Top