C# Convert FromHexString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.FromHexString reads pairs of hex digits and returns a byte[].

Odd-length strings or illegal characters throw. Prefer TryFromHexString for soft failure.

This is a static member of System.Convert. You call it with the type name, such as Convert.FromHexString(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Pass a hex string with an even number of digits.

C#
byte[] data = Convert.FromHexString("0A1B");
byte[] hi = Convert.FromHexString("4869");
CallWhat it does
Convert.FromHexString("0A1B")Decode to bytes {10, 27}.

.NET 5+. Digits are case-insensitive. No spaces/dashes in the basic form.

Library declarations
C#
public static byte[] FromHexString(string s);
public static byte[] FromHexString(ReadOnlySpan<char> chars);

Parameters

ParameterDescription
sRequired. Hex text (even length).

Return Value

Return ValueDescription
byte[]Decoded bytes.

C# Examples

Example 1 — Hex Text to Bytes

Decode 0A1B and print decimal values.

C#
using System;

class Program {
    static void Main() {
        byte[] data = Convert.FromHexString("0A1B");
        Console.WriteLine(string.Join(",", data));
    }
}

How It Works

  1. 0A. Hex A equals decimal 10.
  2. 1B. Hex 1B equals decimal 27.
  3. Join print. Easy console check.
  4. Case free. "0a1b" would decode the same.

Example 2 — Hex Back to Text

Decode 4869 into the string Hi.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        byte[] data = Convert.FromHexString("4869");
        Console.WriteLine(Encoding.UTF8.GetString(data));
    }
}

How It Works

  1. 4869. Hex for ASCII H and i.
  2. FromHexString. Restores two bytes.
  3. GetString. Builds "Hi".
  4. Debug path. Common when reading hex dumps.

Example 3 — ToHex then FromHex

Encode and decode the same payload.

C#
using System;

class Program {
    static void Main() {
        byte[] original = { 1, 2, 255 };
        string hex = Convert.ToHexString(original);
        byte[] back = Convert.FromHexString(hex);
        Console.WriteLine(hex);
        Console.WriteLine(string.Join(",", back));
    }
}

How It Works

  1. Encode. Bytes become 0102FF.
  2. Decode. FromHexString restores them.
  3. 255 stays. FF maps back to 255.
  4. Lossless. Round-trip keeps values.

Frequently Asked Questions

FromHexString throws—hex must come in pairs.
Yes—case-insensitive.
TryFromHexString returns false instead of throwing.
These helpers are .NET 5+; older apps need custom parsing.
Not in the basic string form—strip separators first.

Next: GetTypeCode()

Return the TypeCode for a value or Type with System.Convert.

GetTypeCode() 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.

10 people found this page helpful