Friday, October 24, 2008

Encoding classes

"Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. In contrast, decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters."... (MSDN)

ASCIIEncoding (7-bit ASCII characters. ATENTION: this is NOT a 8-bit ASCII characters conversion!!!)
UTF7Encoding
UTF8Encoding
UnicodeEncoding (UTF-16)
UTF32Encoding

Here an example how to convert one string from Unicode to UTF-8 and back again.

// Create a string that contains Unicode characters.
String unicodeString =
    "This Unicode string contains two characters " +
    "with codes outside the traditional ASCII code range, " +
    "Pi (\u03a0) and Sigma (\u03a3).";

MessageBox.Show(unicodeString);

// Getting unicode string converted to UTF-8 as bytes values
UTF8Encoding utf8Encoding = new UTF8Encoding();
Byte[] utf8Bytes = utf8Encoding.GetBytes(unicodeString);

// These bytes are ready for deliver them through a socket port (TCP/IP)
// or for convert them to one encoded base64 string
string base64string = Convert.ToBase64String(utf8Bytes);
MessageBox.Show(base64string);

// Decode bytes back to string.
// Notice Pi and Sigma characters are still present.
String decodedString = utf8Encoding.GetString(utf8Bytes);
MessageBox.Show(decodedString);

See also:

Conversion between ANSI and Unicode
Encrypting and Decrypting Data

0 comments:


View My Stats