C# Convert ToByte(Int16)
Description
Convert ToByte(Int16)
converts the value of the specified
16-bit signed integer to an equivalent 8-bit unsigned integer.
Syntax
Convert.ToByte(Int16)
has the following syntax.
public static byte ToByte(
short value
)
Parameters
Convert.ToByte(Int16)
has the following parameters.
value
- The 16-bit signed integer to convert.
Returns
Convert.ToByte(Int16)
method returns An 8-bit unsigned integer that is equivalent to value.
Example
The following example converts an array of Int16 values to Byte values.
//w ww . j a va2 s . c o m
using System;
public class MainClass{
public static void Main(String[] argv){
short[] numbers = { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue };
byte result;
foreach (short 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.