C#
string conversion
hex-string
programming
code examples

Convert string to hex-string in C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, converting a string to a hex string really means converting bytes to hexadecimal text. The important question is which bytes you want. If you mean the UTF-8 encoded bytes of the string, use an Encoding first. If you convert character code points directly, the output may differ from what other systems expect.

Convert Text to Bytes First

The most common and portable approach is:

  1. choose an encoding
  2. convert the string to bytes
  3. format each byte as two hex characters
csharp
1using System;
2using System.Text;
3
4string input = "Hello";
5byte[] bytes = Encoding.UTF8.GetBytes(input);
6string hex = Convert.ToHexString(bytes);
7
8Console.WriteLine(hex); // 48656C6C6F

Convert.ToHexString is the cleanest modern API when it is available in your target runtime.

Manual Formatting Works Everywhere

If you want a version that works broadly and makes the mechanics obvious, format each byte yourself.

csharp
1using System;
2using System.Linq;
3using System.Text;
4
5string input = "Hello";
6byte[] bytes = Encoding.UTF8.GetBytes(input);
7string hex = string.Concat(bytes.Select(b => b.ToString("X2")));
8
9Console.WriteLine(hex);

"X2" means uppercase hexadecimal with two characters per byte, including a leading zero when needed.

If you prefer lowercase:

csharp
string hexLower = string.Concat(bytes.Select(b => b.ToString("x2")));

Do Not Confuse Characters with Encoded Bytes

A common mistake is iterating over char values and converting those numeric values directly. That may work for simple ASCII text, but it is not the same as encoding the string for storage or transmission.

csharp
1string input = "é";
2
3string fromChars = string.Concat(input.Select(c => ((int)c).ToString("X4")));
4string fromUtf8 = Convert.ToHexString(Encoding.UTF8.GetBytes(input));
5
6Console.WriteLine(fromChars); // 00E9
7Console.WriteLine(fromUtf8);  // C3A9

Both outputs are valid for different interpretations, but they mean different things. In most interoperability scenarios, you want the encoded-byte version, not the UTF-16 code unit view.

Convert Hex Back to a String

Round-tripping is useful when you need to verify the conversion.

csharp
1using System;
2using System.Text;
3
4string hex = "48656C6C6F";
5byte[] bytes = Convert.FromHexString(hex);
6string text = Encoding.UTF8.GetString(bytes);
7
8Console.WriteLine(text); // Hello

This makes it clear that hex is only a textual representation of bytes, not a different kind of string value.

Start from Raw Bytes When Text Is Not the Real Source

Sometimes the goal is not "convert a human-readable string," but "render some bytes as hex for logging, protocol inspection, or persistence." In that case, skip the string step entirely and format the byte array directly.

csharp
byte[] payload = { 0x01, 0xAB, 0xFF, 0x10 };
string hex = Convert.ToHexString(payload);
Console.WriteLine(hex); // 01ABFF10

This avoids accidental encoding assumptions and is often the better answer when the original data is binary rather than text.

Choose the Encoding Deliberately

UTF-8 is a strong default because it is compact for ASCII-heavy text and interoperates well with other systems. But the right answer depends on the protocol or storage format you are working with.

Possible choices include:

  • 'Encoding.UTF8'
  • 'Encoding.Unicode'
  • 'Encoding.ASCII'
  • a specific legacy code page when interoperability requires it

If the receiver expects UTF-8 and you hex-encode UTF-16 bytes instead, the conversion will look correct locally but fail in the integrated system.

Common Pitfalls

  • Converting char values directly when the real requirement is to encode the string to bytes first.
  • Forgetting to choose an explicit encoding and assuming all systems interpret text the same way.
  • Mixing uppercase and lowercase hex when downstream tools expect one format consistently.
  • Losing leading zeroes by formatting bytes without a fixed two-digit width.
  • Treating hex text as if it were encrypted or otherwise secure when it is only an encoding.

Summary

  • In C#, string-to-hex conversion usually means encode text to bytes and format those bytes in hexadecimal.
  • 'Encoding.UTF8.GetBytes plus Convert.ToHexString is the cleanest modern approach.'
  • Character-code conversion and encoded-byte conversion are not the same thing.
  • Pick the encoding explicitly based on the system you need to interoperate with.
  • If you need confidence in the result, test the round-trip back from hex to text.

Course illustration
Course illustration

All Rights Reserved.