---
title: "405. Convert a Number to Hexadecimal"
url: "https://laigary.com/interview/coding/405-convert-a-number-to-hexadecimal"
type: "note"
section: "coding"
date: "2024-03-10"
updated: "2024-03-10"
tags: ["Bit Manipulation"]
---

# 405. Convert a Number to Hexadecimal

[405\. Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/)

💡

這個題目有一個知識點需要先複習：「[二補數](https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8)」

```python
class Solution:
    def toHex(self, num: int) -> str:
    
        if num == 0:
            return '0'
    
        if num < 0:
            num = (1 << 32) + num
        
        hex_digits = '0123456789abcdef'
        hex_num = ''
        
        while num > 0:
            digit = num % 16
            hex_digit = hex_digits[digit]
            hex_num = hex_digit + hex_num
            num //= 16
        
        return hex_num
```
