Parse string with no format to byte in CSharp
Description
The following code shows how to parse string with no format to byte.
Example
using System;/*from ww w.ja va 2s . com*/
using System.Globalization;
public class Example
{
public static void Main()
{
NumberStyles style;
sbyte number;
string[] values1 = { " 121 ", "121", "-121" };
style = NumberStyles.None;
Console.WriteLine("Styles: {0}", style.ToString());
foreach (string value in values1)
{
try {
number = SByte.Parse(value, style);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
}
}
}
The code above generates the following result.