leetcode 405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. You must not use any method provided by the library which converts/formats the number to hex directly.

 

Example 1:

Input:
26

Output:
"1a"

 

Example 2:

Input:
-1

Output:
"ffffffff"

10进制转换到16进制,其中负数用补码的形式处理

其实,计算机内都是用补码的形式进行计算的。被题目晃了一下。

下面是我愚笨的代码,把10进制转换到二进制然后转换到负数的二进制,然后再转换到16进制。

class Solution {
public:
    char get(int x) {
        if (x < 10) return char(x + '0');
        else {
            switch(x){
                case 10: return 'a';
                case 11: return 'b';
                case 12: return 'c';
                case 13: return 'd';
                case 14: return 'e';
                case 15: return 'f';
            }
        }
    }
    string toHex(int num) {
        if (num == 0) return "0";
        int x = num;
        if (x < 0) x = -x;
        vector<int> v;
        while (x > 0) {
            v.push_back(x%2);
            x /= 2;
        }
        while (v.size() < 32) {
            v.push_back(0);
        }
        string s = "";
        if (num < 0) v[31] = 1;
        else {
            int mark = 0;
            for (int i = 31; i >= 0; i -= 4) {
                int x = 0;    
                for (int j = 3; j >= 0; --j) {
                    if (v[i - j] == 1)x += (int)pow(2.0,3 - j);        
                }
                if (x == 0 && !mark) continue;
                mark = 1;
                s += get(x);
            }
            return s;
        }
        for (int i = 30; i >= 0; --i) {
            v[i] = 1 - v[i];
        }
        for (int i = 0; i < 30; ++i) {
            if (i == 0) {
                v[i] ++;
                if (v[i] >= 2) v[i + 1] ++,v[i] = 0;
                else break;
            } else {
                if (v[i] >= 2) v[i] = 0, v[i + 1]++;
                else break;
            }
        }
        int mark = 0;
        for (int i = 31; i >= 0; i -= 4) {
            int x = 0;    
            for (int j = 3; j >= 0; --j) {
                if (v[i - j] == 1) x += (int)pow(2.0,3 - j);        
            }
            if (x == 0 && !mark) continue;
            mark = 1;
            s += get(x);
        }
        return s;
    }
};

其实计算机中数字本身就是补码的形式表示的,我们只需要直接将其装换到16进制就可以了,转换的思路就是每4个二进制位进行枚举,将这4个二进制转换到0~15就ok了

class Solution {
public:
    string toHex(int num) {
        if(0==num) return "0";
        string res;
        const string HEX = "0123456789abcdef";
        int count=0;
        while(num && 8>count++){
            res = HEX[(num & 0xf)] + res;
            num>>=4;
        }
        return res;
    }
};

 

posted on 2017-08-08 15:26  Beserious  阅读(153)  评论(0编辑  收藏  举报