How to convert an int to a hex string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting an integer to a hexadecimal string is usually a one-line operation because most languages provide a built-in formatter. The real questions are whether you want a prefix such as 0x, uppercase or lowercase digits, fixed-width padding, and how negative numbers should behave.
Hexadecimal is base 16, so each digit represents four bits. That makes it a natural display format for memory addresses, bit masks, colors, binary protocols, and debugging output.
Python
Python provides hex() for the simplest case:
Output:
If you want the value without the 0x prefix, use format specifiers:
"x" gives lowercase, "X" gives uppercase, and "04X" pads to width 4 with leading zeros.
JavaScript
In JavaScript, use toString(16):
If you want a 0x prefix:
For fixed width:
C#
C# uses numeric format strings:
This is the idiomatic approach for application code and logging.
Java
Java has standard helpers too:
For most code, Integer.toHexString or format specifiers are enough.
Padding and Width
A lot of hex formatting questions are really width questions. For example:
- colors often want exactly
6hex digits - bytes often want exactly
2 - 32-bit values often want
8
In Python:
That is often clearer than manually concatenating zeros.
Negative Numbers
Negative integers are where the behavior differs by language and by your intent.
In Python:
Output:
That is a signed representation with a minus sign. But in low-level code you may actually want the two's-complement representation of a fixed-width unsigned integer. In that case, mask the value:
Output:
That distinction matters a lot in systems programming and protocol work.
Manual Conversion Is Rarely Necessary
You can convert an integer to hex manually by repeatedly dividing by 16 and recording remainders, but that is mostly educational now. Built-in formatters are clearer, less error-prone, and handle edge cases more consistently.
Still, the basic idea is:
- divide by
16 - record the remainder as
0throughF - repeat with the quotient
- reverse the collected digits
Understanding that process helps explain why hex aligns so naturally with binary data.
Common Pitfalls
The biggest pitfall is forgetting whether you want the 0x prefix. Some APIs require it, some reject it, and many string comparisons fail because one side includes the prefix while the other does not.
Another common issue is case. Lowercase and uppercase are both valid hex, but protocols, tests, or UI formatting may require one specific style.
Padding is another source of bugs. If you are formatting bytes, color components, or fixed-width machine values, a plain conversion can drop leading zeros that matter to the consumer.
Finally, be explicit about negative numbers. Signed display and fixed-width two's-complement formatting are not the same thing.
Summary
- Most languages have a built-in way to convert integers to hexadecimal strings.
- Use formatting options to control case, prefix, and zero padding.
- Python uses
hex()or format strings such asformat(value, "X"). - JavaScript uses
toString(16), while C# and Java use standard numeric formatters. - Fixed-width output is often more important than the conversion itself.
- Decide how negative numbers should be represented before formatting them.

