Mega Code Archive

 
Categories / C# / Data Types
 

Converts Unicode character to byte

using System; public class MainClass {    public static void Main()    {         char[] chars = {'\x0000', 'a', 'z', '\x0007', '\x03FF' };         foreach (char ch in chars)         {            try {               byte result = Convert.ToByte(ch);               Console.WriteLine("{0} is converted to {1}.", ch, result);            }               catch (OverflowException) {               Console.WriteLine("Unable to convert u+{0} to a byte.",                                  Convert.ToInt16(ch).ToString("X4"));            }         }        } }