How to Turn Bytes into Hexadecimal Text with Convert.ToHexString in C#
Convert.ToHexString(bytes) turns bytes into text with two hexadecimal digits for every byte, in upper case: the bytes 255, 165 and 0 become FFA500. Convert.FromHexString turns the text back into the same bytes.
Microsoft's reference: Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters. An 8-bit unsigned integer is a byte. Hexadecimal writes numbers with the sixteen digits 0 to 9 and A to F, so two of them cover every byte, from 00 to FF.
Syntax
string <hex> = Convert.ToHexString(<bytes>);
string <hex> = Convert.ToHexString(<bytes>, <offset>, <length>);
byte[] <back> = Convert.FromHexString(<hex>);
| Part | What it is |
|---|---|
<bytes> | The bytes to convert: a byte[], or a ReadOnlySpan<byte> in the first form |
<offset> | The index of the first byte to convert. The reference: An offset in inArray. |
<length> | How many bytes to convert. The reference: The number of elements of inArray to convert. |
<hex> | The text: two upper-case hex characters for each byte |
Convert.FromHexString(<hex>) | The reverse: hex text back to a byte[]. The reference: Converts the specified string, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array. |
The Example
A console app called HexString turns the color orange, stored as its red, green and blue bytes, into a hex color code, #FFA500. It also converts four small bytes, then part of the array, and turns the text back into bytes.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n HexString
cd HexString
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/HexString/HexString.csproj:
Determining projects to restore...
Restored /Users/you/source/HexString/HexString.csproj (in 45 ms).
Restore succeeded.
dotnet new console -n HexString creates a folder named HexString with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or HexString.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the color:
// The red, green and blue parts of the color orange, one byte each.
byte[] orange = [255, 165, 0];
// Each byte becomes two hexadecimal digits, in upper case.
string hex = Convert.ToHexString(orange);
Console.WriteLine($"Orange: #{hex}");
orange holds three bytes. Convert.ToHexString(orange) returns one string with two characters for each byte. The # is only part of the printed text.
Then four small bytes:
// Bytes below 16 keep a leading zero, so every byte takes two characters.
byte[] small = [0, 9, 10, 16];
Console.WriteLine($"Small bytes: {Convert.ToHexString(small)}");
A byte from 0 to 15 needs one hex digit, but ToHexString writes two anyway, so the text is always twice as long as the array.
Then part of the array:
// Only part of the array: start at index 1 and take 2 bytes.
Console.WriteLine($"Green and blue: {Convert.ToHexString(orange, 1, 2)}");
The second form takes an offset, the index to start at, and a length, the number of bytes to take: here the green and the blue byte.
Last, back to bytes:
// Convert.FromHexString turns the text back into the same bytes.
byte[] back = Convert.FromHexString(hex);
Console.WriteLine($"Back to bytes: {string.Join(", ", back)}");
Convert.FromHexString reads the text two characters at a time and returns a new byte[]. string.Join prints its bytes with a comma between them.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Orange: #FFA500
Small bytes: 00090A10
Green and blue: A500
Back to bytes: 255, 165, 0

- 255, 165 and 0 became
FFA500:FFis 255,A5is 165 and00is 0. - The small bytes 0, 9, 10 and 16 became
00,09,0Aand10: two characters each, eight in all. - The offset of 1 and length of 2 took the last two bytes,
A500. - FromHexString gave back the same three bytes,
255, 165, 0.
The Complete File

// The red, green and blue parts of the color orange, one byte each.
byte[] orange = [255, 165, 0];
// Each byte becomes two hexadecimal digits, in upper case.
string hex = Convert.ToHexString(orange);
Console.WriteLine($"Orange: #{hex}");
// Bytes below 16 keep a leading zero, so every byte takes two characters.
byte[] small = [0, 9, 10, 16];
Console.WriteLine($"Small bytes: {Convert.ToHexString(small)}");
// Only part of the array: start at index 1 and take 2 bytes.
Console.WriteLine($"Green and blue: {Convert.ToHexString(orange, 1, 2)}");
// Convert.FromHexString turns the text back into the same bytes.
byte[] back = Convert.FromHexString(hex);
Console.WriteLine($"Back to bytes: {string.Join(", ", back)}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Comparing the result with lower-case text | ToHexString writes upper case, so hex == "ffa500" was False | Use Convert.ToHexStringLower, which returned ffa500, or compare with StringComparison.OrdinalIgnoreCase, which returned True |
Using BitConverter.ToString | It puts a dash between the bytes: FF-A5-00 | Use Convert.ToHexString, which gave FFA500. Microsoft's rule CA1872: Use Convert.ToHexString or Convert.ToHexStringLower when encoding bytes to a hexadecimal string representation. |
Building the text with ToString("X") on each byte | It writes as few digits as each byte needs: 0, 9, 10 and 16 became 09A10, three characters short | Use Convert.ToHexString, or ToString("X2") on each byte: both gave 00090A10 |
Passing a # or 0x in front of the digits to FromHexString | #FFA500 threw FormatException: The input is not a valid hex string as its length is not a multiple of 2., and 0xA500 threw FormatException: The input is not a valid hex string as it contains a non-hex character. | Pass the hex digits only: A500 gave 165, 0 |
FAQ
How do I get lower-case hex?
Use Convert.ToHexStringLower, which has the same forms. The reference: Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters. For the orange bytes it returned ffa500, and Convert.FromHexString reads both cases: ffa500 gave back 255, 165, 0.
What happens when the offset and length go past the end?
It throws. Convert.ToHexString(orange, 2, 2) asked for two bytes from index 2 of a three-byte array, and threw ArgumentOutOfRangeException: offset ('2') must be less than or equal to '1'. (Parameter 'offset'). The reference lists this case: offset plus length is greater than the length of inArray.
Conclusion
Convert.ToHexString(bytes) turns bytes into upper-case hex text, two characters for every byte, leading zeros included. Pass an offset and a length to convert part of an array, use Convert.ToHexStringLower for lower case, and give Convert.FromHexString the hex digits alone to get the bytes back.
Additional Resources
- Convert.ToHexString — every form and its exceptions.
- Convert.ToHexStringLower — the same in lower case.
- Convert.FromHexString — hex text back to bytes, and the text it refuses.
- CA1872 — why these methods are preferred over
BitConverter.ToString.
