Wednesday, February 3, 2010

Encoding and Decoding Using UnicodeEncoding in C#

//Include this line in Declaration

UnicodeEncoding Unicode = new UnicodeEncoding();

To Encode a String
-------------------

string inputString = "This string contains the unicode character";
byte[] FilterBytes = Unicode.GetBytes(inputString);
string FilterASCII = System.Text.ASCIIEncoding.ASCII.GetString(FilterBytes);
byte[] AsciiBytes = Unicode.GetBytes(FilterASCII);
string sEncodedText = Convert.ToBase64String(AsciiBytes);

To Decode the Above Encoded String
----------------------------------

byte[] FileTextBytes = Convert.FromBase64String(sEncodedText);
string ASCIItext = Unicode.GetString(FileTextBytes);
byte[] ASCIIBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(ASCIItext);
string sDecodedText = Unicode.GetString(ASCIIBytes);

No comments:

Post a Comment