C# Convert TryFromHexString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.TryFromHexString attempts to decode hex characters into a destination byte span.

Returns false for odd length, illegal digits, or a too-small buffer—no FormatException.

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

Syntax

Pass hex text and a destination byte span.

C#
Span<byte> dest = stackalloc byte[8];
bool ok = Convert.TryFromHexString("0A1B", dest, out int written);
CallWhat it does
Convert.TryFromHexString(hex, dest, out written)true when decode succeeds.

Newer .NET API. Prefer this for untrusted hex dumps.

Library declarations
C#
public static bool TryFromHexString(ReadOnlySpan<char> source,
    Span<byte> destination, out int bytesWritten);

Parameters

ParameterDescription
sourceRequired. Hex characters.
destinationRequired. Byte buffer.
bytesWrittenOut. Bytes written on success.

Return Value

Return ValueDescription
trueDecode succeeded.
falseInvalid hex or buffer too small.

C# Examples

Example 1 — Decode Valid Hex

Try-decode 0A1B into a stack buffer.

C#
using System;

class Program {
    static void Main() {
        Span<byte> dest = stackalloc byte[8];
        bool ok = Convert.TryFromHexString("0A1B", dest, out int written);
        Console.WriteLine(ok);
        Console.WriteLine(string.Join(",", dest.Slice(0, written).ToArray()));
    }
}

How It Works

  1. Valid pairs. Four digits → two bytes.
  2. ok true. Decode filled the span.
  3. 10,27. Decimal view of 0x0A and 0x1B.
  4. Slice written. Ignore unused buffer tail.

Example 2 — False on Odd Digits

Reject a three-character hex string.

C#
using System;

class Program {
    static void Main() {
        Span<byte> dest = stackalloc byte[8];
        bool ok = Convert.TryFromHexString("0A1", dest, out int written);
        Console.WriteLine(ok);
        Console.WriteLine(written);
    }
}

How It Works

  1. Odd length. Hex must come in pairs.
  2. false. Soft failure, no throw.
  3. written 0. Do not read dest.
  4. Validate first. Useful for pasted dumps.

Example 3 — Contrast FromHexString

Show Try success beside a throwing failure.

C#
using System;

class Program {
    static void Main() {
        Span<byte> dest = stackalloc byte[8];
        Console.WriteLine(Convert.TryFromHexString("FF", dest, out _));
        try {
            Convert.FromHexString("ZZ");
        } catch (FormatException) {
            Console.WriteLine("threw");
        }
    }
}

How It Works

  1. FF ok. Try decodes a valid byte.
  2. ZZ illegal. FromHexString throws.
  3. FormatException. Catch prints threw.
  4. Choose Try. When bad hex is common.

Frequently Asked Questions

When hex text may be invalid or incomplete.
Return false (pairs required).
Need at least source.Length / 2 bytes.
TryToHexString writes hex into a char span.
May only have custom hex parsers—these APIs are modern .NET.

Next: TryToBase64Chars()

Try to encode bytes into a Base64 char span without throwing on small buffers with System.Convert.

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

15 people found this page helpful