Create UTF8Encoding, specify whether to provide a Unicode byte order mark : UTF8Encoding « Internationalization I18N « C# / C Sharp






Create UTF8Encoding, specify whether to provide a Unicode byte order mark

  

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);

        Char[] chars = new Char[] {'\uD801', '\uD802'};
        Byte[] bytes = utf8.GetBytes(chars);
        ShowArray(bytes);

        try {
            bytes = utf8ThrowException.GetBytes(chars);
        } catch (Exception e) {
            Console.WriteLine("Exception raised. \nMessage: {0}", e.Message);
        }
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray) {
            Console.WriteLine("[{0}]", o);
        }
    }
}

   
    
  








Related examples in the same category

1.Create UTF8Encoding class
2.UTF8Encoding Class
3.Initializes a new instance of the UTF8Encoding class.
4.Determines whether the specified Object is equal to the current UTF8Encoding object.
5.Calculates the number of bytes produced by encoding a set of characters from the specified character array.
6.Calculates the number of bytes produced by encoding the characters in the specified String.
7.Encodes a set of characters from the specified character array into the specified byte array.
8.Encodes a set of characters from the specified String into the specified byte array.
9.Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
10.Decodes a sequence of bytes from the specified byte array into the specified character array.
11.converts a UTF-8 encoded sequence of bytes into a sequence of Unicode characters.
12.Converts a sequence of Unicode characters into a UTF-8 encoded sequence of bytes.
13.Returns the hash code for the current instance.
14.Calculates the maximum number of bytes produced by encoding the specified number of characters.
15.Calculates the maximum number of characters produced by decoding the specified number of bytes.
16.Returns a Unicode byte order mark encoded in UTF-8 format
17.Convert the passed string to a UTF8 byte array
18.Convert string to byte array back and forth with Encoding.UTF8
19.Convert a string to a Byte Array in UTF8 encoding.
20.Ptr To String Utf8
21.Takes a string and turns it into a base-64 Unicode string