EN
C#/.NET - convert string to byte
8 points
In C#/.NET string can be parsed to byte in few ways.
xxxxxxxxxx
1
string text = "123";
2
byte value = byte.Parse(text);
3
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
xxxxxxxxxx
1
string text = "123";
2
byte value;
3
4
if (byte.TryParse(text, out value))
5
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
xxxxxxxxxx
1
string text = "123";
2
byte value = Convert.ToByte(text);
3
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
xxxxxxxxxx
1
TypeConverter converter = TypeDescriptor.GetConverter(typeof(byte));
2
3
string text = "123";
4
byte value = (byte)converter.ConvertFrom(text);
5
6
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
Check this link: https://dirask.com/q/QD9EaD
The following methodsbool.Parse
,bool.TryParse
,Convert.ToBoolean
andTypeConverter.ConvertFrom
throw System.FormatException.