Given a 32-bit integer (signed or unsigned), convert it to its hexadecimal in lowercase. The result should not have leading zero unless it is equal to zero. Please do not use any inbuilt converting utility. This serves as a coding exercise for beginners.
The elegant solution is to first have a lookup table defined in a const string, and concatenate the remainder in the reverse order, as shown below:
That is: 10767 in decimal equals to 2*16^3 + 10*16^2 + 0*16^1 + 15*16^0.
If the input is zero, we then need to return ‘0’ instead of the empty string.
C++ source code to convert a number to hexadecimal
Given the above explanation, here is the quick C++ function that converts a signed/unsigned 32-bit integer to its Hexadecimal string representation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public: string toHex(int num) { const string table = "0123456789abcdef"; // lookup hexadecimal unsigned int x = num; // make negative complement string r = ""; while (x > 0) { int y = x % 16; // get remainder r = table[y] + r; // concatenate in the reverse order x /= 16; } return r == "" ? "0" : r; } }; |
class Solution { public: string toHex(int num) { const string table = "0123456789abcdef"; // lookup hexadecimal unsigned int x = num; // make negative complement string r = ""; while (x > 0) { int y = x % 16; // get remainder r = table[y] + r; // concatenate in the reverse order x /= 16; } return r == "" ? "0" : r; } };
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
284 wordsloading...
Last Post: How to Implement and Unit Test LineSpace Algorithm in C++?
Next Post: How to Implement The Sgn Function in C++?
The last line is rather inefficiet:
1) you always do the check so you can do it anywhere. Do it early for an early-out. toHex is expected to have a high chance of zero being passed is most programs anyway.
2) the string == “0” is super inefficient. String compare instead of in? No no!
string r = “”; is inefficient anyway. It invokes an empty string copy…!
And PLEASE learn people to use a (static) const char* for the table!!!
1. yes. I agree. You could replace == “0” with r.length() == 0.
2. This is supposed to be a algorithm tutorial, of course there are many code-level optimisations. but I guess the modern compilers are clever enough to figure out these easy stuffs. There is no point in bringing in static keyword which confuses some readers.
No, but compilers can’t optimize away a string class internal’s copy. A const char* is understandable. Anyway, you’re right, good example.
r.length() == 0 is the same as r.empty() ?
yes. i think so.
I think x is always greater than zero since is an unsigned integer. Correct me if i am wrong.
unsigned means non-negative.
so unsigned integer includes zero.
Yes. so the while loop should be x != 0 instead of x greater than zero.
it is the same because x is unsigned.
I also have a similar templated function helper.
constexpr const
array nibleToHexTable {{‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’A’,’B’,’C’,’D’,’E’,’F’}};
template
inline const
string to_hexString(const uint8_t* byteArray, const LengthType& SIZE){
string final_str(“\0”, SIZE*2);
for (LengthType i(0); i > 4];
i+= static_cast(2);
}
return (final_str);
}
Thanks! yes, yours look modern.
I only noticed now. It got weird with the formatting. I don’t know what happened.
You can see the code here:
https://github.com/ClaymorePT/ArchNet/blob/SQLite3_Database/headers/dataTypes/helpers.hpp
Yeah. wordpress comments do not support formatting.