chopstixZ
chopstixZ

Reputation: 3

HttpUtility.UrlDecode encoding from UTF-8 to ASCII

 answers2 = HttpUtility.UrlDecode(Model.FindOne<UserResult>(ur => ur.UserID == user.UserID 
            && ur.ModuleID == 2 
            && ur.TopicID == 9 
            && ur.ActivityID == 2 
            && ur.QuestionID == 2).Results, Encoding.ASCII).Split('|');

The result field contains German characters encoded as ASCII in the SQL database. I'm using a bulk SMS sending service which requires that special characters be sent as ASCII codes.

It's not having difficulty decoding "%7C" and "%20" seeing as the UTF-8 and ASCII codes are the same for it. If I send the character in UTF-8 (%c3%a4) it works fine but if I change it back to ASCII(%E4) the SMS sends me back a question mark in place of the character.

The ASCII decoding scheme i've indicated doesn't seem to be working, i'm not sure what i'm doing wrong.

Upvotes: 0

Views: 1062

Answers (1)

Luaan
Luaan

Reputation: 63732

%E4 is not ASCII. If you want to use values like that, you'll have to use the actual encoding you want to use, e.g.:

Encoding.GetEncoding("iso-8859-2").GetString(new byte[] { 0xE4 })

produces ä, while

Encoding.ASCII.GetString(new byte[] { 0xE4 })

produces ?.

ASCII only describes the first 128 byte-values - the rest are specific encodings that extend ASCII. So any time you try to decode anything larger than 128 by ASCII, you'll get ?.

Obviously, this also works in reverse - any character that's not part of ASCII (and your ä certainly isn't) will be encoded as 63 - also known as ? :)

Upvotes: 1

Related Questions