C# Convert ToByte() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. Text or number to convert.
fromBaseOptional. 2, 8, 10, or 16 when parsing a string.

Return Value

Return ValueDescription
byteResult 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);
    }
}

How It Works

  1. Text digits. "42" becomes byte value 42.
  2. Math works. Adding 1 shows it is numeric, not a string.
  3. Fits range. 42 is safely inside 0–255.
  4. 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));
    }
}

How It Works

  1. Hex FF. Base 16 turns "FF" into 255.
  2. Binary 10. Base 2 turns "10" into 2.
  3. Allowed bases. Use 2, 8, 10, or 16 only.
  4. 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));
    }
}

How It Works

  1. 300 overflows. Bytes stop at 255, so Convert throws.
  2. Catch it. OverflowException lets you print a clear message.
  3. 255 is max. The second call succeeds at the upper edge.
  4. 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.

Next: ToChar()

Convert a value to a single Unicode char with System.Convert.

ToChar() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful