Java solution easy to understand solution with comments


  • 0
    T

    Approach :
    The basic idea is to compare the last four binary digits of the num with 15(F in hexadecimal or 1111 in binary) recursively.

    public String toHex(int num) {
        if(num==0) return "0";
    	String temp = "";
    	while(num!=0){
    		int rem = (num%16)&15;   // compares last four binary digits of the num with F
    		temp = (rem>9 ? (char)('a'-10+rem)+"" : rem+"") + temp; // if remainder is 10, 11 and so on, converts it to 'a' , 'b' and so on..
    		num = num>>>4; // bitwise unsigned right shift by four binary digits
    	}
    	return temp;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.