Convert a Number to Hexadecimal Leetcode Solution

Difficulty Level Easy
Frequently asked in Facebook Microsoft
algorithms Bit Manipulation coding Interview interviewprep LeetCode LeetCodeSolutions

The problem Convert a Number to Hexadecimal Leetcode Solution provides us with an integer. Then asks us to convert the given integer in decimal number system to hexadecimal number system. More formally, the question requires us to convert an integer given in base 10 to a base 16 representation. We had already solved a problem where we were given a number in the decimal number system. And had to convert it into base 7. So, before moving on further, let’s take a look at a few examples.

Example

26
1a

Convert a Number to Hexadecimal Leetcode Solution

Explanation: This conversion is easy, if happen to know about the hexadecimal number system. But if you are unaware of it, just convert the given number into base 16 representation. We do that by repetitive division and storing remainder. One thing to note that, 10 is represented using ‘a’ in hexadecimal notation.

-1
ffffffff

Explanation: Since the negative numbers are stored as their 2’s complement notation. The -1 in its 2s complement notation is 11111111111111111111111111111111. So, we just convert this into hexadecimal which is shown in the output.

Approach for Convert a Number to Hexadecimal Leetcode Solution

Before diving deep into the problem Convert a Number to Hexadecimal Leetcode Solution. Let’s first familiarize ourselves with the hexadecimal number system. So, the hexadecimal number system is also like the decimal number system but the numbers 10 to 15 are represented using lower-case alphabets from ‘a’ to ‘f’. So, we can simply convert an integer in a decimal number system to base 16 representation. And after conversion, we simply replace the numbers 10 – 15 with a – f. But what, do we do with negative numbers? Since negative numbers are stored in 2s complement notation in the binary system. We simply store the number in an unsigned int and just convert it into base 16.

See also
Numbers with prime frequencies greater than or equal to k

The code in Java language also performs the same thing but is implemented in a bit different manner using bit manipulation. So, first we take & of the given number with 15. This operation is equivalent to taking mod with 16. Then using left shift is equivalent to division using 16.

Code

C++ code to Convert a Number to Hexadecimal Leetcode Solution

#include <bits/stdc++.h>
using namespace std;

const string decToHex = "0123456789abcdef";

string toHex(int n){
    if(n==0)
        return "0";
    unsigned int num = n;
    string ans = "";
    while(num > 0){
        ans = decToHex[num%16] + ans;
        num /= 16;
    }
    return ans;
}

int main(){
    cout<<toHex(26);
}
1a

Java code to Convert a Number to Hexadecimal Leetcode Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Solution
{
  public static String toHex(int n) {
        String decToHex = "0123456789abcdef";
        if(n==0)
            return "0";
        int num = n;
        String ans = "";
        while(num != 0){
            ans = decToHex.charAt(num&15) + ans;
            num = num >>> 4;
        }
        return ans;
    }
    
  public static void main (String[] args) throws java.lang.Exception
  {
    System.out.print(toHex(-1));
  }
}
ffffffff

Complexity Analysis

Time Complexity

O(M(n)log n), where n is the length of the given input, M(n) is the time it takes to divide two 2-bit numbers. So, the time complexity is logarithmic.

Space Complexity

O(1), since we had not stored any data regarding each digit in the number. The space complexity is constant.