C# Convert ToByte(Char)

Description

Convert ToByte(Char) converts the value of the specified Unicode character to the equivalent 8-bit unsigned integer.

Syntax

Convert.ToByte(Char) has the following syntax.


public static byte ToByte(
  char value
)

Parameters

Convert.ToByte(Char) has the following parameters.

  • value - The Unicode character to convert.

Returns

Convert.ToByte(Char) method returns An 8-bit unsigned integer that is equivalent to value.

Example

The following example converts an array of Char values to Byte values.


//  w ww.  ja  v  a 2s. c  o  m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    char[] chars = { '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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version