programming
conversion
hexadecimal
integer
coding-tutorials

How to convert an int to a hex string?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

python
value = 255

print(hex(value))

Output:

text
0xff

If you want the value without the 0x prefix, use format specifiers:

python
1value = 255
2
3print(format(value, "x"))   # ff
4print(format(value, "X"))   # FF
5print(format(value, "04X")) # 00FF

"x" gives lowercase, "X" gives uppercase, and "04X" pads to width 4 with leading zeros.

JavaScript

In JavaScript, use toString(16):

javascript
1const value = 255;
2
3console.log(value.toString(16));          // ff
4console.log(value.toString(16).toUpperCase()); // FF

If you want a 0x prefix:

javascript
const hex = "0x" + value.toString(16);
console.log(hex);

For fixed width:

javascript
console.log(value.toString(16).padStart(4, "0")); // 00ff

C#

C# uses numeric format strings:

csharp
1int value = 255;
2
3Console.WriteLine(value.ToString("x"));   // ff
4Console.WriteLine(value.ToString("X"));   // FF
5Console.WriteLine(value.ToString("X4"));  // 00FF

This is the idiomatic approach for application code and logging.

Java

Java has standard helpers too:

java
1int value = 255;
2
3System.out.println(Integer.toHexString(value));              // ff
4System.out.println(Integer.toHexString(value).toUpperCase()); // FF
5System.out.printf("%04X%n", value);                          // 00FF

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 6 hex digits
  • bytes often want exactly 2
  • 32-bit values often want 8

In Python:

python
value = 26
print(f"{value:02X}")  # 1A
print(f"{value:08X}")  # 0000001A

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:

python
value = -42
print(hex(value))

Output:

text
-0x2a

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:

python
value = -42
print(f"{value & 0xFFFFFFFF:08X}")

Output:

text
FFFFFFD6

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:

  1. divide by 16
  2. record the remainder as 0 through F
  3. repeat with the quotient
  4. 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 as format(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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.