C# Convert ToByte(String, IFormatProvider)
Description
Convert ToByte(String, IFormatProvider)
converts
the specified string representation of a number to an equivalent 8-bit unsigned
integer, using specified culture-specific formatting information.
Syntax
Convert.ToByte(String, IFormatProvider)
has the following syntax.
public static byte ToByte(
string value,
IFormatProvider provider
)
Parameters
Convert.ToByte(String, IFormatProvider)
has the following parameters.
value
- A string that contains the number to convert.provider
- An object that supplies culture-specific formatting information.
Returns
Convert.ToByte(String, IFormatProvider)
method returns An 8-bit unsigned integer that is equivalent to value, or zero if value is
null.
Example
using System;//from w w w.j a v a2s .co m
using System.Globalization;
public class Example
{
public static void Main()
{
NumberFormatInfo provider = new NumberFormatInfo();
provider.PositiveSign = "pos ";
provider.NegativeSign = "neg ";
provider.NumberDecimalSeparator = ".";
string[] numericStrings = { "234", "+234", "pos 234", "234.", "255",
"256", "-1" };
foreach (string numericString in numericStrings)
{
Console.Write("'{0,-8}' -> ", numericString);
try {
byte number = Convert.ToByte(numericString, provider);
Console.WriteLine(number);
}
catch (FormatException) {
Console.WriteLine("Incorrect Format");
}
catch (OverflowException) {
Console.WriteLine("Overflows a Byte");
}
}
}
}
The code above generates the following result.