Developer Guide

C# Hex to String with Convert.FromHexString

C# hex → string conversion is: parse hex into a byte[], then decode with Encoding (usually UTF-8). Below are practical options from modern .NET down to compatible helpers — without padding the list with obsolete APIs.

Try samples with Hex to String. Concepts: What is hexadecimal encoding?.

Preferred: Convert.FromHexString (.NET 5+)

using System.Text;

byte[] bytes = Convert.FromHexString("48656C6C6F");
string text = Encoding.UTF8.GetString(bytes); // Hello

string hex = Convert.ToHexString(Encoding.UTF8.GetBytes("Hello"));

Fast, clear, and built-in. Rejects invalid characters and odd length.

Span-friendly parsing

static byte[] ParseHex(ReadOnlySpan<char> hex)
{
    hex = hex.Trim();
    if (hex.Length % 2 != 0) throw new FormatException("odd length");
    byte[] buffer = new byte[hex.Length / 2];
    for (int i = 0; i < buffer.Length; i++)
    {
        buffer[i] = byte.Parse(hex.Slice(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
    }
    return buffer;
}

Useful when you want allocation control or to strip spaces first with your own normalization step.

Legacy-friendly loop

static byte[] HexToBytes(string hex)
{
    hex = hex.Replace(" ", "", StringComparison.Ordinal);
    if (hex.Length % 2 != 0) throw new ArgumentException("odd length");
    var bytes = new byte[hex.Length / 2];
    for (int i = 0; i < bytes.Length; i++)
        bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
    return bytes;
}

string s = Encoding.UTF8.GetString(HexToBytes("e4bda0e5a5bd"));

What to avoid

Encoding reminder

// Same bytes, different encodings → different text
var bytes = HexToBytes("c3a9"); // UTF-8 for 'é'
Console.WriteLine(Encoding.UTF8.GetString(bytes));
// Encoding.GetEncoding(1252).GetString(bytes) is a different character story

See also: Java · JavaScript · String to Hex tool.