Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long long decimalToBinary(int n);
- string binaryToHex(string binary);
- long long OctalToBinary(int num);
- int BinaryToDecimal(string BinaryNum);
- int OctalToDecimal(int num);
- int convertBinarytoOctal(long long binaryNumber);
- long long DecimalToOctal(long long decimal);
- string binaryToHex(string binary)
- {
- // 1 1 1 0
- // 2^3 2^2 2^1 2^0
- // 8*1 + 4*1 + 2*1 + 0
- // 14 >> 'E'
- char arr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- vector<char> hex;
- string part1 = ""; // binary number before '.' , if binary="1001.1101" >> part1="1001"
- string part2 = ""; // binary number after '.' , if binary="1001.1101" >> part2="1101"
- for (int i = 0; i < binary.length(); i++)
- {
- if (binary[i] == '.')
- {
- part2 = binary.substr(1 + binary.find('.'), binary.length());
- break;
- }
- part1 += binary[i];
- }
- if (part1.length() > 0)
- {
- int sum = 0, j = 0;
- reverse(part1.begin(), part1.end());
- for (int i = 0; i < part1.length(); i++)
- {
- sum += (part1[i] - '0') * (pow(2, j));
- j++;
- if (j == 4)
- {
- hex.push_back(arr[sum]);
- sum = 0;
- j = 0;
- }
- }
- if (sum != 0)
- {
- hex.push_back(arr[sum]);
- }
- reverse(hex.begin(), hex.end());
- }
- if (part2.length() > 0)
- {
- hex.push_back('.');
- int sum = 0, j = 3;
- for (int i = 0; i < part2.length(); i++)
- {
- sum += (part2[i] - '0') * (pow(2, j));
- j--;
- if (j == -1)
- {
- hex.push_back(arr[sum]);
- sum = 0;
- j = 3;
- }
- }
- if (sum != 0)
- {
- hex.push_back(arr[sum]);
- }
- }
- string res = ""; // Hexadecimal number
- for (int i = 0; i < hex.size(); i++)
- {
- res += hex[i];
- }
- return res;
- }
- long long OctalToBinary(int num)
- {
- int decimal = OctalToDecimal(num);
- return decimalToBinary(decimal);
- }
- int BinaryToDecimal(string BinaryNum)
- {
- int sz = (int)BinaryNum.size();
- int DecimalNum = 0;
- for (int i = 0; i < sz; i++)
- {
- if (BinaryNum[sz - 1 - i] == '1')
- DecimalNum += pow(2, i);
- }
- return DecimalNum;
- }
- int OctalToDecimal(int num)
- {
- int x = 0;
- int result = 0;
- while (num > 0)
- {
- int y = num % 10; // The right-most digit
- num /= 10;
- result += y * pow(8, x);
- ++x;
- }
- return result;
- }
- int convertBinarytoOctal(long long binaryNumber)
- {
- int octalNumber = 0, decimalNumber = 0, i = 0;
- while (binaryNumber != 0)
- {
- decimalNumber += (binaryNumber % 10) * pow(2, i);
- ++i;
- binaryNumber /= 10;
- }
- i = 1;
- while (decimalNumber != 0)
- {
- octalNumber += (decimalNumber % 8) * i;
- decimalNumber /= 8;
- i *= 10;
- }
- return octalNumber;
- }
- long long DecimalToOctal(long long decimal)
- {
- long long digit = 1;
- long long decNum = decimal;
- long long octal = 0;
- while (decimal != 0)
- {
- octal += (decimal % 8) * digit;
- digit *= 10;
- decimal /= 8;
- }
- return octal;
- }
- long long decimalToBinary(int n)
- {
- long long ans = 0;
- int remainder, i = 1;
- while (n != 0)
- {
- remainder = n % 2;
- ans += remainder * i;
- i = i * 10;
- n = n / 2;
- }
- return ans;
- }
- int main()
- {
- char c = 'y';
- cout << "******************** Number Base Conversion *********************\n";
- while (c == 'y')
- {
- int num;
- 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;
- cin >> num;
- if (num == 1)
- {
- cout << "Enter Binary number to convert into its Decimal : ";
- string s;
- cin >> s;
- cout << "Decimal Number of ( " << s << " ) is ( " << BinaryToDecimal(s) << " )" << endl;
- }
- if (num == 2)
- {
- cout << "Enter Binary number to convert into its Octal : ";
- long long s;
- cin >> s;
- cout << "Octal Number of ( " << s << " ) is ( " << convertBinarytoOctal(s) << " )" << endl;
- }
- if (num == 3)
- {
- cout << "Enter Binary number to convert into its Hexadecimal : ";
- string s;
- cin >> s;
- cout << "Hexadecimal Number of ( " << s << " ) is ( " << binaryToHex(s) << " )" << endl;
- }
- if (num == 4)
- {
- cout << "Enter Decimal number to convert into its Binary : ";
- int s;
- cin >> s;
- cout << "Binary Number of ( " << s << " ) is ( " << decimalToBinary(s) << " )" << endl;
- }
- if (num == 5)
- {
- cout << "Enter Decimal number to convert into its Octal : ";
- long long s;
- cin >> s;
- cout << "Octal Number of ( " << s << " ) is ( " << DecimalToOctal(s) << " )" << endl;
- }
- if (num == 6)
- {
- cout << "Enter Octal number to convert into its Binary : ";
- int s;
- cin >> s;
- cout << "Binary Number of ( " << s << " ) is ( " << OctalToBinary(s) << " )" << endl;
- }
- if (num == 7)
- {
- cout << "Enter Octal number to convert into its Decimal : ";
- int s;
- cin >> s;
- cout << "Decimal Number of ( " << s << " ) is ( " << OctalToDecimal(s) << " )" << endl;
- }
- cout << "\nperform another operation (y/n) : ";
- cin >> c;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement