How to use the UTF8Encoding class to encode and decode a text?

The steps to use the UTF8Encoding class to encode and decode a text data are as follows:

  1. Implement the following namespace:

     

    using System.Text;

  2. Create a UTF8Encoding class object as follows:

    UTF8Encoding utf8 = new UTF8Encoding();

  3. 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)”;

  4. Encode the given text in the following manner:

    Byte[] encodedText = utf8.GetBytes(unicodeString);

  5.  

  6. Display the encoded text as follows:

    foreach (Byte bt in encodedText)
    Console.Write(“[{0}]“, bt);

  7. Decode the encoded text as follows:

    String decodeStr = utf8.GetString(encodedText);

  8. Display the decoded text as follows:

    Console.WriteLine(decodedStr);

你可能感兴趣的:(How to use the UTF8Encoding class to encode and decode a text?)