I recently was faced with a problem where a form was sending text encoded in UTF-8, and the ASP.Net page processing the data used "windows-1256". This really messed up the text.
The dilemma I was that I had to keep the page encoding containing the form as utf-8, and I had to keep the processing page as windows-1256.
I was able to solve it by converting the incoming text from utf-8 to windows-1256 by using the following two methods:
///<summary> /// Converts any UTF-8 string into the selected character set ///</summary> ///<param name="StringToConvert"></param> ///<param name="TargetCharSet"></param> ///<returns></returns> public byte[] ConvertUTF8StringEncoding(string StringToConvert, string TargetCharSet) { byte[] ByteConvertedString; byte[] ByteStringToConvert; Encoding TargetEncoding; // Convert the string to a Byte array ByteStringToConvert = Encoding.UTF8.GetBytes(StringToConvert); // Get the target encoding type TargetEncoding = Encoding.GetEncoding(TargetCharSet); // Convert the byte array using the target encoding ByteConvertedString = Encoding.Convert(System.Text.Encoding.UTF8, TargetEncoding, ByteStringToConvert); return ByteConvertedString; } ///<summary> /// This converts the Binary array back to string. ///</summary> ///<param name="Binary"></param> ///<returns></returns> string BinaryUTF8ToString(byte[] Binary) { return System.Text.UnicodeEncoding.UTF8.GetString(Binary); } |
The first method will return a byte array of the string encoded in windows-1256. The second method will convert the binary array back to its string value.
So to convert any UTF-8 form input string, we write the following
stringConvertedString = BinaryUTF8ToString(ConvertStringEncoding( Request.Form["FormInput"], "windows-1256")); |
April, 29, 2008 10:04 AM