Solution Share, C++ 0ms with reinterpret_cast


  • 0
    J

    My idea is to convert the signed int to unsigned to avoid complex manipulation, so reinterpret_cast is used.My code is as follows:

    class Solution {
    public:
        string mp = "0123456789abcdef";
        string toHex(int num) {
            if (num == 0)   return "0";
            string hex;
            unsigned x = * (reinterpret_cast<unsigned* > (&num));
            while (x) {
                hex = mp[x % 16] + hex;
                x /= 16;
            }
            return hex;
        }
    };
    

Log in to reply
 

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