C# Convert ToByte(String, Int32)
Description
Convert ToByte(String, Int32)
converts the string representation
of a number in a specified base to an equivalent 8-bit unsigned integer.
Syntax
Convert.ToByte(String, Int32)
has the following syntax.
public static byte ToByte(
string value,
int fromBase
)
Parameters
Convert.ToByte(String, Int32)
has the following parameters.
value
- A string that contains the number to convert.fromBase
- The base of the number in value, which must be 2, 8, 10, or 16.
Returns
Convert.ToByte(String, Int32)
method returns An 8-bit unsigned integer that is equivalent to the number in value, or 0 (zero)
if value is null.
Example
The following example alternately attempts to interpret an array of strings as the representation of binary, octal, decimal, and hexadecimal values.
using System;// w w w . j a v a 2 s . c o m
public class Example
{
public static void Main()
{
int[] bases = { 2, 8, 10, 16 };
string[] values = { "-1", "1", "08", "0F", "11" , "12", "30",
"101", "255", "FF", "10000000", "80" };
byte number;
foreach (int numBase in bases)
{
Console.WriteLine("Base {0}:", numBase);
foreach (string value in values)
{
try {
number = Convert.ToByte(value, numBase);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (Exception) {
}
}
}
}
}
The code above generates the following result.