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