C# byte array to hex string
last modified October 5, 2026
In this article, we show how to convert a byte array to a hexadecimal string in C#.
The byte type is an unsigned integral value type whose values range from 0 to 255. It is commonly used for binary data read from files, streams, and network connections.
Hexadecimal is a base-16 numbering system. It uses the digits 0 to 9 and the letters A to F (or a to f) to represent values from 0 to 15. One hexadecimal digit represents four binary bits, so one byte is conventionally written as two hexadecimal digits.
Hexadecimal format specifier
The hexadecimal format specifier X or x converts a number to a string
of hexadecimal digits. An uppercase specifier produces A-F; a lowercase specifier produces a-f.
A precision specifier sets the minimum number of digits. If necessary, zeros are added on the left. The precision does not truncate a value that needs more digits.
int n = 2350;
Console.WriteLine(n.ToString("X"));
Console.WriteLine(n.ToString("x"));
Console.WriteLine(n.ToString("X2"));
Console.WriteLine(n.ToString("X4"));
Console.WriteLine(n.ToString("X8"));
The program formats an integer as hexadecimal using several format specifiers.
$ dotnet run 92E 92e 92E 092E 0000092E
The following example converts a text string to a byte array and formats each byte with two
uppercase hexadecimal digits. Encoding.UTF8 is used because text can contain characters
that are not representable by ASCII.
using System.Linq;
using System.Text;
string msg = "an old falcon";
byte[] data = Encoding.UTF8.GetBytes(msg);
string hex = string.Join(" ", data.Select(x => x.ToString("X2")));
Console.WriteLine(hex);
The ToString("X2") format keeps every byte two digits wide. The spaces are added by
string.Join; they are for readability and are not part of the byte values.
$ dotnet run 61 6E 20 6F 6C 64 20 66 61 6C 63 6F 6E
C# Convert.ToHexString
The Convert.ToHexString method, available in .NET 5 and later, converts a byte array
to a string containing uppercase hexadecimal characters. It is the simplest choice for a compact
hexadecimal representation without separators.
using System.Text; string msg = "an old falcon"; byte[] data = Encoding.UTF8.GetBytes(msg); string hex = Convert.ToHexString(data); Console.WriteLine(hex);
The method writes two hexadecimal characters for each byte and uses uppercase letters.
$ dotnet run 616E206F6C642066616C636F6E
C# BitConverter.ToString
The BitConverter.ToString method converts each byte to a two-character hexadecimal
value and separates the values with hyphens. It is useful when the separated form is convenient
for logging or diagnostics.
using System.Text;
string msg = "an old falcon";
byte[] data = Encoding.UTF8.GetBytes(msg);
string hex = BitConverter.ToString(data);
Console.WriteLine(hex);
string hexWithSpaces = hex.Replace('-', ' ');
Console.WriteLine(hexWithSpaces);
The second line replaces the hyphens with spaces. Unlike Convert.ToHexString,
BitConverter.ToString includes separators in its result.
$ dotnet run 61-6E-20-6F-6C-64-20-66-61-6C-63-6F-6E 61 6E 20 6F 6C 64 20 66 61 6C 63 6F 6E
C# hex string to byte array
The Convert.FromHexString method reverses the operation. It accepts a string containing
an even number of hexadecimal digits and returns the corresponding byte array. It throws a
FormatException for invalid characters or an odd number of digits.
string hex = "616E206F6C642066616C636F6E"; byte[] data = Convert.FromHexString(hex); Console.WriteLine(System.Text.Encoding.UTF8.GetString(data));
$ dotnet run an old falcon
C# binary file to hex string
The File.ReadAllBytes method reads the contents of a file into a byte array and
closes the file after reading it. The following example prints the bytes of a binary file in
uppercase hexadecimal, ten bytes per line.
var path = "favicon.ico";
byte[] data = File.ReadAllBytes(path);
for (int i = 0; i < data.Length; i++)
{
Console.Write($"{data[i]:X2} ");
if ((i + 1) % 10 == 0)
{
Console.WriteLine();
}
}
The exact output depends on the contents of favicon.ico. The ellipsis below indicates
that the output continues; the file must exist in the program's working directory.
$ dotnet run 00 00 01 00 01 00 10 10 00 00 00 00 00 00 68 05 00 00 16 00 00 00 28 00 00 00 10 00 00 00 ...
In this article, we have shown several ways to convert a byte array to a hexadecimal string and how to convert a hexadecimal string back to a byte array.
Source
Convert.ToHexString method - .NET API
BitConverter.ToString method - .NET API
The X format specifier - .NET documentation
Author
List all C# tutorials.