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");
Call
What 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
Parameter
Description
s
Required. Hex text (even length).
Return Value
Return Value
Description
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));
}
}
Output
10,27
How It Works
0A. Hex A equals decimal 10.
1B. Hex 1B equals decimal 27.
Join print. Easy console check.
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));
}
}
Output
Hi
How It Works
4869. Hex for ASCII H and i.
FromHexString. Restores two bytes.
GetString. Builds "Hi".
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));
}
}
Output
0102FF
1,2,255
How It Works
Encode. Bytes become 0102FF.
Decode. FromHexString restores them.
255 stays. FF maps back to 255.
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.