Click here to Skip to main content
11,923,958 members (65,899 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET encoding UTF-8
Hello friends,

I need to escape URI data string using ISO-8859-15 encoding.
As I am using this code, the output is still escaped by using UTF-8 encoding, since Uri.EscapeDataString is internally using UTF-8 encoding.
var srcEnc = Encoding.Unicode;
var uniString = "This is a test £.";
var srcBytes = Encoding.Unicode.GetBytes(uniString);
var dstEnc = Encoding.GetEncoding("ISO-8859-15"); //
var dstBytes = Encoding.Convert(srcEnc, dstEnc, srcBytes);
var dstString = dstEnc.GetString(dstBytes);
var data = Uri.EscapeDataString(dstString); // output should be "This%20is%20a%20test%20%a3." (%a3 for £)
One workaround would be to take all ISO bytes (dstBytes) and manually format each byte like "%xx", but for debugging reasons this is not acceptable.
I cannot reference System.Web assembly.

Thank you for any help.
Posted 19-Mar-13 4:57am
Sergey Alexandrovich Kryukov 19-Mar-13 10:01am
How come you have this encoding in first place? It is obsolete. Why not to transcode data to Unicode (UTF-8) first?
Matej Hlatky 19-Mar-13 10:21am
Hi. This encoding is required by some gateway we are using. It's not my choice.
Sergey Alexandrovich Kryukov 19-Mar-13 10:39am
I'm not asking about it. I'm asking why.
Matej Hlatky 19-Mar-13 11:29am
The 3rd party gateway is used for sending SMS.
When I send escaped utf8 string, while the gateway is expecting escaped string in iso encoding, the incoming SMS contains additional chars.
Gateway additionaly supports sending of utf8 encoded texts (which is the solution in fact), but this can be problem if destination mobile network does not supports
Sergey Alexandrovich Kryukov 19-Mar-13 12:06pm
That is nasty. Anyway, do you have a problem converting to ISO.. ? Unfortunately, I don't know how such think can be used in URI or, say, escaped. If this is a usual, '%' escaping (URL encoding), it should not be a problem.

Do you have any correctly working samples of URI for this gateway, with ISO-8859-15 encoding, escaped or whatever. Can you show such sample?

Matej Hlatky 19-Mar-13 12:12pm
Sample escaped iso string is on the last line of my code example in comment ("This%20is%20a%20test%20%a3."). Anyway, I implemented iso encode method by myself.
Thank you for your time.
Sergey Alexandrovich Kryukov 19-Mar-13 12:15pm
This is not a representative sample. How about non-ASCII code points? I answered the question tentatively, please try.
I'll appreciate your feedback. Will it work for you or not.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I cannot be sure that what I suggest will work, because this situation with your gateway looks quite pathological to me.

However, I strongly suspect that you simply have to percent-encode all characters beyond ASCII with and pass all characters with ASCII subset (up to code point 127) as is. Just look and compare:[^],[^].

Just for a record: even if you quite redundantly percent-encode each character in the URI, it will work; the only problem is its readability for a user.

And the escaping should be done using this schema:[^].

In addition to the characters shown in tables, percent-encode all characters with code points above 127.

Just give it a try.
Maciej Los 19-Mar-13 12:26pm
Sergey, i like your comments, for example this: " situation with your gateway looks quite pathological to me".
Sergey Alexandrovich Kryukov 19-Mar-13 12:32pm
Thank you, Maciej. It looks like we have characteristic Easter European picky attitudes... :-)
I hope my guesswork worked out for OP who accepted it.
Maciej Los 19-Mar-13 12:44pm
It's not Eastern European picky attitude... Your remarks are totally in of character. As mine ;)
Sergey Alexandrovich Kryukov 19-Mar-13 13:35pm
Matej Hlatky 19-Mar-13 12:43pm
Thank you again for your time. Your answer is basically the solution which I used and it works like a charm. :)
Sergey Alexandrovich Kryukov 19-Mar-13 13:35pm
My pleasure.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151125.3 | Last Updated 19 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100