Convert Unicode character to byte in CSharp
Description
The following code shows how to convert Unicode character to byte.
Example
// w w w .j av a 2 s . c om
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"));
}
}
}
}
The code above generates the following result.