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:
- choose an encoding
- convert the string to bytes
- format each byte as two hex characters
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.
"X2" means uppercase hexadecimal with two characters per byte, including a leading zero when needed.
If you prefer lowercase:
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.
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.
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.
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
charvalues 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.GetBytesplusConvert.ToHexStringis 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.

