diff options
Diffstat (limited to 'num_to_hex_binary.cpp')
-rw-r--r-- | num_to_hex_binary.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/num_to_hex_binary.cpp b/num_to_hex_binary.cpp new file mode 100644 index 0000000..81470fd --- /dev/null +++ b/num_to_hex_binary.cpp @@ -0,0 +1,55 @@ +#include "leetcode.h" +#include <string> + +/* + * given integer 'num', return string representing hexidecimal value. + * for negative numbers, use two's complement method + */ + +class Solution { +public: + string toHex(int num) { + string ans; + while (num) { + int rem = num % 16; + if (rem > 9) + switch (rem) { + case 10: + ans = 'a' + ans; + break; + case 11: + ans = 'b' + ans; + break; + case 12: + ans = 'c' + ans; + break; + case 13: + ans = 'd' + ans; + break; + case 14: + ans = 'e' + ans; + break; + case 15: + ans = 'f' + ans; + break; + } + else + ans = char(rem + 48) + ans; + num /= 16; + } + return ans; + } + int toBinary(int num) { + int ans = 0; + return ans; + } +}; + +int main() { + Solution obj; + // obj.toHex(26); + cout << obj.toHex(26) << endl; // expect: "1a" + cout << obj.toHex(-1) << endl; // expect: "fffffff" + cout << obj.toBinary(26) << endl; // expect: 11010 + cout << obj.toBinary(-1) << endl; // expect: +} |