1823. Hexadecimal Dump
A debugger prints 32-bit register values in hexadecimal. Given the signed integer num, return how the register holds it, written in base 16 with the lowercase letters a to f for the digits 10 to 15. A negative number is shown as its 32-bit two's complement, so -1 becomes eight letters f.
The text has no leading zeros, except that the value 0 is written as 0. Do not use any built-in hexadecimal formatting. A loop that takes the lowest four bits and then shifts right, treating the value as unsigned, runs in constant time because a 32-bit value has at most eight hexadecimal digits.
Example 1
- Input:
- num = 26
- Output:
- "1a"
- Explanation:
26 is 1*16 + 10, and 10 is written a, so the result is 1a.
Example 2
- Input:
- num = -1
- Output:
- "ffffffff"
- Explanation:
-1 has all 32 bits set, which is eight f digits.
Example 3
- Input:
- num = 0
- Output:
- "0"
- Explanation:
Zero is written as a single 0.
Constraints
-231 ≤ num ≤ 231 - 1
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(1)
- Space
- O(1)