Watch 10 video solutions for Convert a Number to Hexadecimal, a easy level problem involving Math, Bit Manipulation. This walkthrough by code Explainer has 7,285 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a 32-bit integer num, return a string representing its hexadecimal representation. For negative integers, two’s 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.
Example 1:
Input: num = 26 Output: "1a"
Example 2:
Input: num = -1 Output: "ffffffff"
Constraints:
-231 <= num <= 231 - 1Problem Overview: Convert a signed 32-bit integer into its hexadecimal string representation without using built-in conversion utilities. For negative numbers, the result must follow the two's complement representation used by computers.
Approach 1: Bitwise Iteration Method (O(1) time, O(1) space)
This approach works directly with bit manipulation. A hexadecimal digit represents exactly 4 bits, so you can repeatedly extract the last 4 bits of the number using a bitmask 0xf. The operation num & 0xf isolates the lowest hex digit. Convert that value (0–15) to its corresponding character (0–9 or a–f), append it to the result, then shift the number right by 4 bits using num >>> 4 (unsigned shift in languages that support it). This continues until the number becomes zero or up to 8 hex digits are processed for a 32‑bit integer. The key insight is that masking and shifting mimic how hexadecimal is stored at the binary level, making the conversion extremely efficient. Since a 32‑bit integer produces at most 8 hex digits, the runtime is effectively constant. This method is preferred in interviews because it demonstrates strong understanding of bit operations and two's complement representation.
Approach 2: Divide and Conquer Method – Recursive (O(log16 n) time, O(log16 n) space)
This approach uses classic base conversion from math. Divide the number by 16 to reduce the problem size, then recursively process the quotient until the base case is reached. Each recursion step handles one hexadecimal digit by computing num % 16, mapping the remainder to its hex character. The recursive call builds the higher‑order digits first, and the remainder contributes the current digit. Because each step divides the number by 16, the recursion depth equals the number of hex digits, which is O(log16 n). This version is easier to reason about conceptually because it mirrors the mathematical definition of base conversion. However, it requires careful handling of negative numbers to preserve the correct two's complement representation, and recursion introduces additional stack space.
Recommended for interviews: The bitwise iteration method is what most interviewers expect. It shows you understand how hexadecimal aligns with binary (4 bits per digit) and how masking and shifting extract values efficiently. The recursive divide‑by‑16 approach demonstrates the general base‑conversion idea and is useful for explaining the concept first, but the iterative bitwise solution better showcases practical knowledge of bit manipulation and low‑level number representation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Bitwise Iteration Method | O(1) | O(1) | Best general solution. Efficient for fixed-size integers and demonstrates bit manipulation knowledge. |
| Divide and Conquer (Recursive) | O(log16 n) | O(log16 n) | Useful for explaining base conversion conceptually or when recursion is preferred. |