Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
var srcEnc = Encoding.Unicode;
var uniString = "This is a test £.";
var srcBytes = Encoding.Unicode.GetBytes(uniString);

var dstEnc = Encoding.GetEncoding("ISO-8859-15"); // http://en.wikipedia.org/wiki/ISO/IEC_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
Comments
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?
—SA
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.
—SA
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
it.
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?

—SA

1 solution

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:
http://en.wikipedia.org/wiki/ISO-8859-15[^],
http://en.wikipedia.org/wiki/ASCII[^].

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: http://en.wikipedia.org/wiki/URL_encoding[^].

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

Just give it a try.
—SA
 
Share this answer
 
Comments
Maciej Los 19-Mar-13 12:26pm    
Sergey, i like your comments, for example this: " situation with your gateway looks quite pathological to me".
+5!
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.
—SA
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. :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900