C# Convert ToHexString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.ToHexString turns each byte into two hex digits (0–9, A–F).

It ships with .NET 5 and later. For older frameworks, format bytes manually or use BitConverter style helpers.

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

Syntax

Pass the bytes to encode.

C#
byte[] data = { 0x0A, 0x1B };
string hex = Convert.ToHexString(data);  // "0A1B"
CallWhat it does
Convert.ToHexString(data)Encode the whole array as hex.
Convert.ToHexString(data.AsSpan(0, 1))Encode a span slice (overloads vary by TFM).

Output uses uppercase hex digits. Pair with FromHexString to decode.

Library declarations
C#
public static string ToHexString(byte[] bytes);
public static string ToHexString(ReadOnlySpan<byte> bytes);

Parameters

ParameterDescription
bytesRequired. Bytes to encode as hex.

Return Value

Return ValueDescription
stringHex text with two digits per byte.

C# Examples

Example 1 — Bytes to Hex Text

Encode two bytes and print the hex string.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 0x0A, 0x1B };
        Console.WriteLine(Convert.ToHexString(data));
    }
}

How It Works

  1. Two bytes. 0x0A and 0x1B keep the sample tiny.
  2. ToHexString. Emits two digits per byte.
  3. 0A1B. Uppercase hex characters.
  4. No separators. Digits are contiguous.

Example 2 — Hex of "Hi"

Encode UTF-8 bytes of a short word.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        byte[] data = Encoding.UTF8.GetBytes("Hi");
        Console.WriteLine(Convert.ToHexString(data));
      }
}

How It Works

  1. UTF-8. "H" is 0x48, "i" is 0x69.
  2. Hex text. Becomes 4869.
  3. Readable dump. Handy for debugging payloads.
  4. Round-trip. FromHexString can restore the bytes.

Example 3 — Empty Array Edge

Show encoding an empty byte array.

C#
using System;

class Program {
    static void Main() {
        byte[] data = Array.Empty<byte>();
        string hex = Convert.ToHexString(data);
        Console.WriteLine(hex.Length);
        Console.WriteLine(hex == "");
    }
}

How It Works

  1. No bytes. Empty input is valid.
  2. Length 0. Result string is empty.
  3. Equals "". Confirms empty text.
  4. Safe default. Useful when optional payloads are missing.

Frequently Asked Questions

ToHexString requires .NET 5 or newer.
ToHexString uses uppercase A–F.
No—anyone can decode it.
Convert.FromHexString or TryFromHexString.
The API returns contiguous digits; add dashes yourself if needed.

Next: ToInt16()

Convert a value to short (Int16, −32768…32767) with System.Convert.

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

12 people found this page helpful