# Simple Java solution with comment

• ``````/*
Basic idea: each time we take a look at the last four digits of
binary verion of the input, and maps that to a hex char
shift the input to the right by 4 bits, do it again
until input becomes 0.

*/

public class Solution {

char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

public String toHex(int num) {
if(num == 0) return "0";
String result = "";
while(num != 0){
result = map[(num & 15)] + result;
num = (num >>> 4);
}
return result;
}

}``````````

• My corresponding C++ code. I must add result.size()<8 in the while condition, or it will loop forever with a negative input.

``````class Solution {
public:
string toHex(int num) {
if (num == 0) return "0";
vector<char> toHexChar = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
string result;
while (num != 0 && result.size()<8){
result = toHexChar[num&15] + result;
num = num >> 4;
}
return result;
}
};
``````

• StringBuilder may be used here.

``````    public String toHex(int num) {
StringBuilder sb = new StringBuilder();
do {
int n = num & 0xf;
n += n < 0xa ? '0' : 'a' - 10;
sb.append((char)n);
} while ((num >>>= 4) != 0);
return sb.reverse().toString();
}
``````

• char[] may be used here.

``````        char[] ans = new char[8];
int offset = 8;
do {
int n = num & 0xf;
n += n > 9 ? 'a' - 10 : '0';
ans[--offset] = (char)n;
num >>>= 4;
} while (num != 0);
return new String(ans, offset, 8 - offset);
``````

• ``````            result = map[(num & 0x0F)] + result;
``````

15 replace 0x0F

• @truffleyang

Your solution is right. However a "more correct" way to do unsgined right shift, IMO, is using an unsigned value, in which way the `.size()` doesn't needed to be checked and the code is similar to that in Java.

``````class Solution {
public:
string toHex(int num) {
if (num == 0) return "0";
unsigned int n = static_cast<unsigned int>(num);

vector<char> map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
string result;

while (n != 0) {
result.push_back(map[n & 0b1111]);
n >>= 4;
}

std::reverse(result.begin(), result.end());
return result;

}
};
``````

• I like your user name

• Make programmers great again?!

• Thanks Mr. Trump.

• Awesome Solution, Thx a lot!!!

• Thank you for making computer science great again

• @DonaldTrump I hope you become President Mr Trump. This solution is great, even "tremendous". I believe you will bring similarly tremendous solutions to our country currently governed by the "corrupt and stupid" politicians.

• Donald Trump, I hope that you can offer us more H1B friendly policies just like this answer. I would vote for you.

• Thanks President Trump!

• @truffleyang FYI no need to check size everytime. You can do a logical shift in c++ by casting num to unsigned int.

``````   while (num != 0) {
result = toHexChar[num&15] + result;
num = (unsigned int) num >> 4;
}``````

• Thanks Mr. Trump.

Shocked by OP's name.

• @s I approve this message. #MAGA

• I think we should take negative number into consideration, add the code as following before while loop.
if num < 0:
num = -num
num ^= 0xffffffff
num += 1

• @木的3次方
The solution has already taken negative input into consideration by using the unsigned shift ">>>" as opposed to ">>"

• How to come up with using 15 to do the & operation with the num?
I know how it works but don't know how it is obtained.
Can someone help clarify that?
Thx,

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