Advertisement
Guest User

Base Converter

a guest
Dec 6th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6.  
  7. int strToDecimal(int base, string num);
  8.  
  9.  
  10. string decimalToBase(int number, int base);
  11.  
  12.  
  13. int main()
  14.  
  15. {
  16.  
  17. int menuChoice;
  18.  
  19. int startBase;
  20.  
  21. int numberInDecimal;
  22.  
  23. string number;
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. cout << "NUMBER BASE CONVERTER 2019" << endl;
  32. cout << "By: Suraj Joshi" << endl << endl;
  33.  
  34. cout << "Enter number to convert: ";
  35. cin >> number;
  36.  
  37. cout << "Choose your starting base: " << endl
  38. << "1. Binary" << endl
  39. << "2. Octal" << endl
  40. << "3. Decimal" << endl
  41. << "4. Hex" << endl;
  42.  
  43. cin >> menuChoice;
  44.  
  45. switch(menuChoice)
  46.  
  47. {
  48. case 1:
  49. {
  50.  
  51. startBase = 2;
  52. break;
  53.  
  54. }
  55. case 2:
  56. {
  57.  
  58. startBase = 8;
  59. break;
  60.  
  61. }
  62. case 3:
  63. {
  64.  
  65. startBase = 10;
  66. break;
  67.  
  68. }
  69. case 4:
  70. {
  71.  
  72. startBase = 16;
  73. break;
  74.  
  75. }
  76. default:
  77. {
  78.  
  79. startBase = 0;
  80. cout << "Invalid Choice..." << endl;
  81. break;
  82.  
  83. }
  84. }
  85.  
  86.  
  87. numberInDecimal = strToDecimal(startBase, number);
  88.  
  89. cout << "Binary: " << decimalToBase(numberInDecimal, 2) << endl;
  90. cout << "Decimal: " << numberInDecimal << endl;
  91. cout << "Octal: " << decimalToBase(numberInDecimal, 8) << endl;
  92. cout << "Hex: " << decimalToBase(numberInDecimal, 16) << endl;
  93.  
  94.  
  95.  
  96. return 0;
  97. }
  98.  
  99.  
  100. // This is where the problem starts(I Believe) I never experianced the problem when this wasnt here
  101. int strToDecimal(int base, string num)
  102.  
  103. {
  104.  
  105. int sum = 0;
  106.  
  107. for (int i = 0; num.length() - 1; ++i) {
  108.  
  109. if(num[i] > 64)
  110.  
  111. sum += (num[i] - 55) * pow(base, num.length() - 1 - i);
  112.  
  113. else
  114.  
  115. sum += (num[i] - 48) * pow(base, num.length() - 1 - i);
  116.  
  117. }
  118.  
  119.  
  120. return sum;
  121.  
  122. }
  123.  
  124. // this can be ingored, This isnt what is causing the problem but feel free to look at it, it isnt complete yet
  125. string decimalToBase(int number, int base )
  126. {
  127. int rem;
  128. string tempStr (1, number % base + 48);
  129.  
  130. while(number != 0){
  131.  
  132. rem = number % base;
  133. number = number / base;
  134.  
  135. // str.insert(0, string(1, num % base + 48))
  136. // or string tempStr (1, num % base + 48);
  137. //str.insert(0, tempStr);
  138.  
  139. switch(rem)
  140. {
  141.  
  142.  
  143.  
  144.  
  145. } // switch
  146.  
  147. } // while
  148.  
  149.  
  150.  
  151. return " ";
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement