Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

C#/.NET - convert string to byte

0 contributions
8 points

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.
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more