In C#, Convert.ToByte returns a byte—an unsigned 8-bit integer from 0 to 255.
Pass digit text, another numeric type, or an object. Values outside 0–255 throw OverflowException. Unreadable text throws FormatException.
This is a static member of System.Convert. You call it with the type name, such as Convert.ToByte(...). Convert is the everyday API for converting between common CLR types in C#.
Syntax
Convert text or numbers into a byte.
C#
byte a = Convert.ToByte("200");
byte b = Convert.ToByte(10);
byte c = Convert.ToByte("FF", 16);
Call
What it does
Convert.ToByte("200")
Decimal digit string → byte.
Convert.ToByte(10)
Int value that fits in 0–255.
Convert.ToByte("FF", 16)
Hex string with base 16.
Like other numeric To* methods, Convert.ToByte(null) returns 0. Prefer byte.TryParse for untrusted text.
Library declarations
C#
public static byte ToByte(string? value);
public static byte ToByte(string? value, int fromBase);
public static byte ToByte(int value);
Parameters
Parameter
Description
value
Required. Text or number to convert.
fromBase
Optional. 2, 8, 10, or 16 when parsing a string.
Return Value
Return Value
Description
byte
Result in the range 0–255.
C# Examples
Example 1 — Digit String to Byte
Convert a decimal string into a byte.
C#
using System;
class Program {
static void Main() {
byte b = Convert.ToByte("42");
Console.WriteLine(b);
Console.WriteLine(b + 1);
}
}
Output
42
43
How It Works
Text digits."42" becomes byte value 42.
Math works. Adding 1 shows it is numeric, not a string.
Fits range. 42 is safely inside 0–255.
Null note. A null string argument would yield 0 instead of throwing.
Example 2 — Parse Hex with fromBase
Read a hex digit pair using base 16.
C#
using System;
class Program {
static void Main() {
byte b = Convert.ToByte("FF", 16);
Console.WriteLine(b);
Console.WriteLine(Convert.ToByte("10", 2));
}
}
Output
255
2
How It Works
Hex FF. Base 16 turns "FF" into 255.
Binary 10. Base 2 turns "10" into 2.
Allowed bases. Use 2, 8, 10, or 16 only.
Still a byte. Results must fit 0–255 or overflow throws.
Example 3 — Guard Against Large Values
Wrap ToByte when the source might exceed 255.
C#
using System;
class Program {
static void Main() {
try {
Console.WriteLine(Convert.ToByte(300));
} catch (OverflowException) {
Console.WriteLine("too big for byte");
}
Console.WriteLine(Convert.ToByte(255));
}
}
Output
too big for byte
255
How It Works
300 overflows. Bytes stop at 255, so Convert throws.
Catch it.OverflowException lets you print a clear message.
255 is max. The second call succeeds at the upper edge.
Validate first. For user input, TryParse is often safer.
Frequently Asked Questions
0 through 255 inclusive.
OverflowException is thrown.
Yes for typical numeric ToByte overloads.
Yes—pass fromBase 16, e.g. Convert.ToByte("A0", 16).
Parse throws on null; Convert returns 0. Both throw on bad format.