> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Convert a Number to Hexadecimal

> Tested Python solution for LeetCode 405 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 405, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 405   # by problem number
lcpy gen -s convert_a_number_to_hexadecimal   # by problem name
```

## Problem

Given a 32-bit integer `num`, return *a string representing its hexadecimal representation*. For negative integers, [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) method is used.

All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.

**Note:** You are not allowed to use any built-in library method to directly solve this problem.

### Examples

```
Input: num = 26
Output: "1a"
```

```
Input: num = -1
Output: "ffffffff"
```

### Constraints

* -2^31 \<= num \<= 2^31 - 1

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/convert_a_number_to_hexadecimal/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/convert_a_number_to_hexadecimal/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1) - at most 8 hex digits for a 32-bit integer
    # Space: O(1) - output string holds at most 8 characters
    def to_hex(self, num: int) -> str:
        value = num & 0xFFFFFFFF
        digits = "0123456789abcdef"
        if value == 0:
            return "0"
        out: list[str] = []
        while value:
            out.append(digits[value & 0xF])
            value >>= 4
        return "".join(reversed(out))
```

## Complexity

| Time | Space |
| - | - |
| O(1) - at most 8 hex digits for a 32-bit integer | O(1) - output string holds at most 8 characters |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.