Parse string to byte with number style setting
using System;
using System.Globalization;
public class MainClass
{
public static void Main()
{
string byteString;
NumberStyles styles;
byteString = "1024";
styles = NumberStyles.Integer;
CallTryParse(byteString, styles);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
Byte byteValue;
bool result = Byte.TryParse(stringToConvert, styles,
null as IFormatProvider, out byteValue);
if (result){
Console.WriteLine("Converted '{0}' to {1}",stringToConvert, byteValue);
}else{
Console.WriteLine("Attempted conversion of '{0}' failed.", stringToConvert.ToString());
}
}
}
Related examples in the same category