Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Convert.ToByte Method (Char)

Converts the value of the specified Unicode character to the equivalent 8-bit unsigned integer.

Namespace:  System
Assemblies:   System.Runtime.Extensions (in System.Runtime.Extensions.dll)
  mscorlib (in mscorlib.dll)

public static byte ToByte(
	char value
)

Parameters

value
Type: System.Char

The Unicode character to convert.

Return Value

Type: System.Byte
An 8-bit unsigned integer that is equivalent to value.

ExceptionCondition
OverflowException

value represents a number that is greater than Byte.MaxValue.

This method returns an unsigned byte value that represents the numeric code of the Char object passed to it. In the .NET Framework, a Char object is a 16-bit value. This means that the method is suitable for returning the numeric codes of characters in the ASCII character range or in the Unicode C0 Controls and Basic Latin, and C1 Controls and Latin-1 Supplement ranges, from U+0000 to U+00FF.

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

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 example displays the following output: 
//       a is converted to 97. 
//       z is converted to 122. 
//        is converted to 7. 
//       Unable to convert u+03FF to a byte.      

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft