Parse string with a culture-specific format to byte with IFormatProvider
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
byte byteValue;
string stringToConvert = " 214 ";
try {
byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
stringToConvert, Byte.MaxValue, Byte.MinValue);
}
}
}
Related examples in the same category