Advertisement
NickAndNick

Base10 => Base2, Base8, Base16

Mar 13th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. template<typename Type> constexpr bool is_integer =
  5.     is_same<Type, long long>::value || is_same<Type, unsigned long long>::value ||
  6.     is_same<Type, long>::value || is_same<Type, unsigned long>::value ||
  7.     is_same<Type, int>::value || is_same<Type, unsigned>::value ||
  8.     is_same<Type, short>::value || is_same<Type, unsigned short>::value;
  9.  
  10. template<typename Integer> auto convert_base(Integer number, const int base) {
  11.     static_assert(is_integer<Integer>, "The value must be an integer type!");
  12.     const char digits[] = "0123456789ABCDEF";
  13.     string box;
  14.     auto save = number;
  15.     if (number < 0) {
  16.         auto type = string(typeid(number).name());
  17.         if (type == "short"s) {
  18.             auto n = static_cast<unsigned short>(abs(numeric_limits<Integer>::min() - number)) + numeric_limits<Integer>::max() + 1;
  19.             while (n) {
  20.                 box = digits[n % base] + box;
  21.                 n /= base;
  22.             }
  23.         }
  24.         if (type == "int"s) {
  25.             auto n = static_cast<unsigned>(abs(numeric_limits<Integer>::min() - number)) + numeric_limits<Integer>::max() + 1;
  26.             while (n) {
  27.                 box = digits[n % base] + box;
  28.                 n /= base;
  29.             }
  30.         }
  31.         if (type == "long"s) {
  32.             auto n = static_cast<unsigned long>(abs(numeric_limits<Integer>::min() - number)) + numeric_limits<Integer>::max() + 1;
  33.             while (n) {
  34.                 box = digits[n % base] + box;
  35.                 n /= base;
  36.             }
  37.         }
  38.         if (type == "__int64"s) {
  39.             auto n = static_cast<unsigned __int64>(abs(numeric_limits<Integer>::min() - number)) + numeric_limits<Integer>::max() + 1;
  40.             while (n) {
  41.                 box = digits[n % base] + box;
  42.                 n /= base;
  43.             }
  44.         }
  45.     } else {
  46.         while (number) {
  47.             box = digits[number % base] + box;
  48.             number /= base;
  49.         }
  50.     }
  51.     if (base == 2) box = "0b" + box;
  52.     else if (base == 8) box = '0' + box;
  53.     else if (base == 16) box = "0x" + box;
  54.     else box = to_string(save);
  55.     return box;
  56. }
  57. int main() {
  58.     long a = 127;
  59.     auto ab = convert_base(a, 2);
  60.     auto ao = convert_base(a, 8);
  61.     auto ah = convert_base(a, 16);
  62.     cout << ab << '\n' << ao << '\n' << ah << '\n';
  63.     long long b = -127;
  64.     auto bb = convert_base(b, 2);
  65.     auto bo = convert_base(b, 8);
  66.     auto bh = convert_base(b, 16);
  67.     cout << bb << '\n' << bo << '\n' << bh << '\n';
  68.     auto x = 555;
  69.     auto xx = convert_base(x, 12);
  70.     cout << xx << '\n';
  71.     cin.get();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement