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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var