Wednesday, August 1, 2012

asp.net - URL Decode Chinese Characters

I had to work on task where I had to decode a  file with a list of encoded strings.The person who asked me to decode the file also told me that the strings where a bunch of chineese characters and that he did not know how to decode it.
After trying different encodings finally I was able to figure out the encoding "gbk" with which I was able to decode the file.
Here is the code


protected void Page_Load(object sender, EventArgs e)
{
     using(StreamReader oReader = new StreamReader(@"C:\Data\Sample.csv"))
     {
           string sHeader = oReader.ReadLine();
           while (!oReader.EndOfStream)
           {
                string[] sList = oReader.ReadLine().Split(',');
                if (sList[6].Contains('%'))
                {               Response.Write(HttpUtility.UrlDecode(sList[6],Encoding.GetEncoding("gbk")));
                     Response.Write("</br>");
                }
            }
     }
 }


No comments: