Advertisement
semsem_elazazy

Numbering System Conversions

May 15th, 2023
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. long long decimalToBinary(int n);
  4. string binaryToHex(string binary);
  5. long long OctalToBinary(int num);
  6. int BinaryToDecimal(string BinaryNum);
  7. int OctalToDecimal(int num);
  8. int convertBinarytoOctal(long long binaryNumber);
  9. long long DecimalToOctal(long long decimal);
  10.  
  11. string binaryToHex(string binary)
  12. {
  13.     // 1    1    1   0
  14.     // 2^3  2^2  2^1  2^0
  15.     // 8*1 + 4*1 +  2*1 +  0
  16.     // 14 >> 'E'
  17.     char arr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  18.     vector<char> hex;
  19.  
  20.     string part1 = ""; // binary number before '.' , if binary="1001.1101" >> part1="1001"
  21.     string part2 = ""; // binary number after '.' , if binary="1001.1101" >> part2="1101"
  22.     for (int i = 0; i < binary.length(); i++)
  23.     {
  24.         if (binary[i] == '.')
  25.         {
  26.             part2 = binary.substr(1 + binary.find('.'), binary.length());
  27.             break;
  28.         }
  29.  
  30.         part1 += binary[i];
  31.     }
  32.  
  33.     if (part1.length() > 0)
  34.     {
  35.         int sum = 0, j = 0;
  36.         reverse(part1.begin(), part1.end());
  37.         for (int i = 0; i < part1.length(); i++)
  38.         {
  39.  
  40.             sum += (part1[i] - '0') * (pow(2, j));
  41.             j++;
  42.  
  43.             if (j == 4)
  44.             {
  45.                 hex.push_back(arr[sum]);
  46.                 sum = 0;
  47.                 j = 0;
  48.             }
  49.         }
  50.         if (sum != 0)
  51.         {
  52.             hex.push_back(arr[sum]);
  53.         }
  54.         reverse(hex.begin(), hex.end());
  55.     }
  56.     if (part2.length() > 0)
  57.     {
  58.         hex.push_back('.');
  59.         int sum = 0, j = 3;
  60.         for (int i = 0; i < part2.length(); i++)
  61.         {
  62.  
  63.             sum += (part2[i] - '0') * (pow(2, j));
  64.             j--;
  65.             if (j == -1)
  66.             {
  67.                 hex.push_back(arr[sum]);
  68.                 sum = 0;
  69.                 j = 3;
  70.             }
  71.         }
  72.         if (sum != 0)
  73.         {
  74.             hex.push_back(arr[sum]);
  75.         }
  76.     }
  77.     string res = ""; // Hexadecimal number
  78.  
  79.     for (int i = 0; i < hex.size(); i++)
  80.     {
  81.         res += hex[i];
  82.     }
  83.     return res;
  84. }
  85.  
  86. long long OctalToBinary(int num)
  87. {
  88.     int decimal = OctalToDecimal(num);
  89.     return decimalToBinary(decimal);
  90. }
  91.  
  92. int BinaryToDecimal(string BinaryNum)
  93. {
  94.     int sz = (int)BinaryNum.size();
  95.     int DecimalNum = 0;
  96.     for (int i = 0; i < sz; i++)
  97.     {
  98.         if (BinaryNum[sz - 1 - i] == '1')
  99.             DecimalNum += pow(2, i);
  100.     }
  101.     return DecimalNum;
  102. }
  103.  
  104. int OctalToDecimal(int num)
  105. {
  106.     int x = 0;
  107.     int result = 0;
  108.  
  109.     while (num > 0)
  110.     {
  111.         int y = num % 10; // The right-most digit
  112.         num /= 10;
  113.  
  114.         result += y * pow(8, x);
  115.         ++x;
  116.     }
  117.  
  118.     return result;
  119. }
  120.  
  121. int convertBinarytoOctal(long long binaryNumber)
  122. {
  123.     int octalNumber = 0, decimalNumber = 0, i = 0;
  124.  
  125.     while (binaryNumber != 0)
  126.     {
  127.         decimalNumber += (binaryNumber % 10) * pow(2, i);
  128.         ++i;
  129.         binaryNumber /= 10;
  130.     }
  131.  
  132.     i = 1;
  133.  
  134.     while (decimalNumber != 0)
  135.     {
  136.         octalNumber += (decimalNumber % 8) * i;
  137.         decimalNumber /= 8;
  138.         i *= 10;
  139.     }
  140.  
  141.     return octalNumber;
  142. }
  143.  
  144. long long DecimalToOctal(long long decimal)
  145. {
  146.     long long digit = 1;
  147.     long long decNum = decimal;
  148.     long long octal = 0;
  149.     while (decimal != 0)
  150.     {
  151.         octal += (decimal % 8) * digit;
  152.         digit *= 10;
  153.         decimal /= 8;
  154.     }
  155.     return octal;
  156. }
  157. long long decimalToBinary(int n)
  158. {
  159.     long long ans = 0;
  160.     int remainder, i = 1;
  161.     while (n != 0)
  162.     {
  163.         remainder = n % 2;
  164.         ans += remainder * i;
  165.         i = i * 10;
  166.         n = n / 2;
  167.     }
  168.  
  169.     return ans;
  170. }
  171. int main()
  172. {
  173.     char c = 'y';
  174.     cout << "******************** Number Base Conversion *********************\n";
  175.     while (c == 'y')
  176.     {
  177.         int num;
  178.         cout << "1.Binary to Decimal\n2.Binary to Octal\n3.Binary to Hexadecimal\n4.Decimal to Binary\n5.Decimal to Octal\n6.Octal to Binary\n7.Octal to Decimal\nYour choice : " << endl;
  179.         cin >> num;
  180.         if (num == 1)
  181.         {
  182.             cout << "Enter Binary number to convert into its Decimal : ";
  183.             string s;
  184.             cin >> s;
  185.             cout << "Decimal Number of ( " << s << " ) is ( " << BinaryToDecimal(s) << " )" << endl;
  186.         }
  187.         if (num == 2)
  188.         {
  189.             cout << "Enter Binary number to convert into its Octal : ";
  190.             long long s;
  191.             cin >> s;
  192.             cout << "Octal Number of ( " << s << " ) is ( " << convertBinarytoOctal(s) << " )" << endl;
  193.         }
  194.         if (num == 3)
  195.         {
  196.             cout << "Enter Binary number to convert into its Hexadecimal : ";
  197.             string s;
  198.             cin >> s;
  199.             cout << "Hexadecimal Number of ( " << s << " ) is ( " << binaryToHex(s) << " )" << endl;
  200.         }
  201.         if (num == 4)
  202.         {
  203.             cout << "Enter Decimal number to convert into its Binary : ";
  204.             int s;
  205.             cin >> s;
  206.             cout << "Binary Number of ( " << s << " ) is ( " << decimalToBinary(s) << " )" << endl;
  207.         }
  208.         if (num == 5)
  209.         {
  210.             cout << "Enter Decimal number to convert into its Octal : ";
  211.             long long s;
  212.             cin >> s;
  213.             cout << "Octal Number of ( " << s << " ) is ( " << DecimalToOctal(s) << " )" << endl;
  214.         }
  215.         if (num == 6)
  216.         {
  217.             cout << "Enter Octal number to convert into its Binary : ";
  218.             int s;
  219.             cin >> s;
  220.             cout << "Binary Number of ( " << s << " ) is ( " << OctalToBinary(s) << " )" << endl;
  221.         }
  222.         if (num == 7)
  223.         {
  224.             cout << "Enter Octal number to convert into its Decimal : ";
  225.             int s;
  226.             cin >> s;
  227.             cout << "Decimal Number of ( " << s << " ) is ( " << OctalToDecimal(s) << " )" << endl;
  228.         }
  229.         cout << "\nperform another operation (y/n) : ";
  230.         cin >> c;
  231.     }
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement