Languages
[Edit]
EN

C#/.NET - convert string to byte

8 points
Created by:
Pearl-Hurley
559

In C#/.NET string can be parsed to byte in few ways.

1. byte.Parse example

string text = "123";
byte value = byte.Parse(text);
Console.WriteLine(value);

Output:

123

2. byte.TryParse example

string text = "123";
byte value;

if (byte.TryParse(text, out value))
	Console.WriteLine(value);

Output:

123

3. Convert.ToByte example

string text = "123";
byte value = Convert.ToByte(text);
Console.WriteLine(value);

Output:

123

4. TypeConverter.ConvertFrom example

TypeConverter converter = TypeDescriptor.GetConverter(typeof(byte));

string text = "123";
byte value = (byte)converter.ConvertFrom(text);

Console.WriteLine(value);

Output:

123

5. Custom conversion example

Check this link: https://dirask.com/q/QD9EaD

6. Notes

The following methods bool.Parse, bool.TryParse, Convert.ToBoolean and TypeConverter.ConvertFrom throw System.FormatException.
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - string conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join