C# Convert ToByte(UInt16)

Description

Convert ToByte(UInt16) converts the value of the specified 16-bit unsigned integer to an equivalent 8-bit unsigned integer.

Syntax

Convert.ToByte(UInt16) has the following syntax.


[CLSCompliantAttribute(false)]
public static byte ToByte(
  ushort value
)

Parameters

Convert.ToByte(UInt16) has the following parameters.

  • value - The 16-bit unsigned integer to convert.

Returns

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

Example

The following example converts an array of unsigned 16-bit integers to Byte values.


// w w  w.  j av a2s. c  om
using System;
public class MainClass{
  public static void Main(String[] argv){  
    ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue };
    byte result;
    foreach (ushort number in numbers)
    {
       try {
          result = Convert.ToByte(number);
          Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", 
                            number.GetType().Name, number, 
                            result.GetType().Name, result);
       }                     
       catch (OverflowException) {
          Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", 
                            number.GetType().Name, number);
       }
    }

  }
}
    

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