int
hex
string conversion
programming
data types

How to convert an Int to 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 already provide built-in formatting for base 16. The part that actually causes confusion is not the conversion itself, but the surrounding choices: uppercase or lowercase, whether to include the 0x prefix, and how negative values should be represented.

The Core Idea

Hexadecimal is base 16, so each digit represents one of sixteen values from 0 through F. Because one hex digit maps neatly to four bits, hex is a compact way to display binary-oriented data such as memory addresses, color values, flags, and hashes.

For a positive integer, the process is conceptually simple:

  1. divide by 16 repeatedly
  2. record each remainder
  3. map values 10 through 15 to A through F
  4. reverse the collected digits

In real code, you should almost always use the language runtime's formatter rather than writing this by hand.

Common Built-In Conversions

Python

Python's built-in hex() returns a lowercase hex string with the 0x prefix:

python
1value = 255
2print(hex(value))      # 0xff
3print(format(value, "x"))   # ff
4print(format(value, "X"))   # FF

If you want uppercase without the prefix, format(value, "X") is the clearest option.

C#

In C#, numeric formatting strings are the normal solution:

csharp
1using System;
2
3int value = 255;
4
5Console.WriteLine(value.ToString("x"));   // ff
6Console.WriteLine(value.ToString("X"));   // FF
7Console.WriteLine($"0x{value:X}");        // 0xFF

You can also control zero-padding:

csharp
int value = 255;
Console.WriteLine(value.ToString("X4"));  // 00FF

JavaScript

JavaScript uses toString(16):

javascript
const value = 255;
console.log(value.toString(16));          // ff
console.log("0x" + value.toString(16));   // 0xff

For uppercase, add a string conversion step:

javascript
console.log(value.toString(16).toUpperCase()); // FF

Negative Integer Behavior

Negative numbers are where many developers get surprised. Languages do not all display them the same way.

In Python:

python
print(hex(-42))  # -0x2a

Python treats the minus sign as a sign on the number, not as a request for a fixed-width two's-complement bit pattern.

In C#, formatting a signed integer in hex shows the underlying bit pattern:

csharp
1using System;
2
3int value = -42;
4Console.WriteLine(value.ToString("X"));   // FFFFFFD6 on 32-bit int formatting

That can be correct and still surprising if you expected -2A. So before converting negative values, decide whether you want:

  • a signed mathematical representation
  • the raw machine-style bit pattern

Those are different outputs.

Manual Conversion Example

Built-ins are usually best, but understanding the manual algorithm helps explain the output:

python
1def to_hex(n: int) -> str:
2    if n == 0:
3        return "0"
4
5    digits = "0123456789ABCDEF"
6    result = []
7    value = n
8
9    while value > 0:
10        value, remainder = divmod(value, 16)
11        result.append(digits[remainder])
12
13    return "".join(reversed(result))
14
15
16print(to_hex(26))
17print(to_hex(255))
18print(to_hex(4095))

This implementation handles positive integers only, which is usually enough to illustrate the mechanics clearly. Production code should still prefer a built-in formatter unless you have a special requirement.

Choosing a Representation

Before you settle on an output format, decide a few details:

  • Do you need uppercase or lowercase digits
  • Should the result include the 0x prefix
  • Do you need fixed width such as 00FF
  • How should negative integers behave

Those decisions matter in APIs, logs, network protocols, and binary tooling because consumers often expect a very specific format.

Common Pitfalls

The biggest pitfall is assuming all languages format negative integers the same way. They do not.

Another pitfall is manually concatenating prefixes and padding without checking whether the built-in formatter already has options for that job. Most runtimes already support width and case control.

Developers also confuse base conversion with byte serialization. A hex string is a textual representation, not a raw byte array.

Finally, if the output is meant for another system, confirm the exact format contract first. ff, FF, and 0xFF may all represent the same number to a human, but not necessarily to a parser.

Summary

  • Converting an integer to hex is usually a built-in formatting operation.
  • The real choices are prefix, case, width, and negative-number behavior.
  • Python, C#, and JavaScript all offer simple built-in hex conversion methods.
  • C# and other languages may show negative signed integers as two's-complement bit patterns.
  • Use manual conversion only when you need custom behavior that built-in formatters do not provide.

Course illustration
Course illustration

All Rights Reserved.