C# Byte Parse(String, NumberStyles)
Description
Byte Parse(String, NumberStyles)
converts the string
representation of a number in a specified style to its Byte equivalent.
Syntax
Byte.Parse(String, NumberStyles)
has the following syntax.
public static byte Parse(
string s,
NumberStyles style
)
Parameters
Byte.Parse(String, NumberStyles)
has the following parameters.
s
- A string that contains a number to convert. The string is interpreted using the style specified by style.style
- A bitwise combination of enumeration values that indicates the style elements that can be present in s. A typical value to specify is NumberStyles.Integer.
Returns
Byte.Parse(String, NumberStyles)
method returns A byte value that is equivalent to the number contained in s.
Example
The following example parses string representations of Byte values with the Byte.Parse(String, NumberStyles) method. The current culture for the example is en-US.
using System;//from w w w .j a va 2 s .c om
using System.Globalization;
public class MainClass {
public static void Main(String[] argv){
string value;
NumberStyles style;
byte number;
// Parse value with no styles allowed.
style = NumberStyles.None;
value = " 241 ";
try
{
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Parse value with trailing sign.
style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
value = " 163+";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// Parse value with leading sign.
value = " +253 ";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
}
The code above generates the following result.