The steps to use the UTF8Encoding class to encode and decode a text data are as follows:
- Implement the following namespace:
using System.Text;
- Create a UTF8Encoding class object as follows:
UTF8Encoding utf8 = new UTF8Encoding();
- Create a string object to store input text as follows:
String unicodeString =”This unicode string contains two characters with codes outside an 8-bit code range,Pi (u03a0) and Sigma (u03a3)”;
- Encode the given text in the following manner:
Byte[] encodedText = utf8.GetBytes(unicodeString);
- Display the encoded text as follows:
foreach (Byte bt in encodedText)
Console.Write(“[{0}]“, bt); - Decode the encoded text as follows:
String decodeStr = utf8.GetString(encodedText);
- Display the decoded text as follows:
Console.WriteLine(decodedStr);