This is a C++ class which has a member function that takes 3 arguments and converts the number from first base to second base. It takes the following arguments.
– std::string thestlstring
– int base
– int base2
The number is stored in the form of std::string and returns the number as string . It supports bases 2 till base 16.
Source code is contributed by: massiveattack92(at)hotmail.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <cmath> #include <string> #include <sstream> #include "inputs.h" inputs::inputs(std::string newstring, int newbase, int newbase2) { inpthenumber = newstring; inpbase = newbase; inpbase2 = newbase2; } inputs::~inputs(void) {} std::string inputs::conv(std::string thestlstring, int base, int base2) { // variable declarations int myval; int remainder; int loopvar = 0; int mynumber = 0; int testsum = 0; float value1; float value2; char mychar; char element; std::string stlstring; std::stringstream thestream; // generate base 10 integer from number in base 'mybase' string::iterator pos; pos = thestlstring.end(); for (--pos; pos >= thestlstring.begin(); --pos) { mychar = * pos; switch (mychar) { case '0': myval = 0; break; case '1': myval = 1; break; case '2': myval = 2; break; case '3': myval = 3; break; case '4': myval = 4; break; case '5': myval = 5; break; case '6': myval = 6; break; case '7': myval = 7; break; case '8': myval = 8; break; case '9': myval = 9; break; case 'A': myval = 10; break; case 'B': myval = 11; break; case 'C': myval = 12; break; case 'D': myval = 13; break; case 'E': myval = 14; break; case 'F': myval = 15; break; } mynumber += myval * (int) std::pow(base, loopvar); ++loopvar; } // generate 'n' - (n-1) = # of digits in base 10 number // generated above for (int n = 0; testsum < mynumber; ++n) { testsum = testsum + (int) std::pow(base2, n) * (base2 - 1); } // declare array for insertion of digits from the // converted number char * revarray = new char[n - 1]; // generate the new number for (int i = 0; i < n; ++i) { value1 = (float)((float) mynumber / (float) base2); value2 = (float) floor((float) mynumber / (float) base2); remainder = mynumber - (int) value2 * base2; if (remainder < 10) element = char(remainder + '0'); else if (remainder == 10) element = 'A'; else if (remainder == 11) element = 'B'; else if (remainder == 12) element = 'C'; else if (remainder == 13) element = 'D'; else if (remainder == 14) element = 'E'; else if (remainder == 15) element = 'F'; // insert an element each time within the loop revarray[i] = element; // move down one base multiple mynumber = (mynumber - remainder) / base2; } // create std::string format of converted number for (int i2 = n - 1; i2 >= 0; --i2) { thestream << revarray[i2]; } thestream >> stlstring; // return the number return stlstring; } //---------------------- //inputs.h //---------------------- #include <string> class inputs { private: std::string inpthenumber; int inpbase; int inpbase2; public: inputs(std::string, int, int); // called // automatically when instance created ~inputs(void); // automatically called // when object released from RAM // must return void. std::string conv(std::string thestlstring, int base, int base2); }; |
Sample Output
Input: thestlstring = “777”, base = 8, base2 = 10
Output: 511
Explanation: 777 in octal (base =8) when converted to decimal (base =10) is 511.
Input: thestlstring = “927”, base = 10, base2 = 16
Output: 39F
Explanation: 927 in decimal (base =10) when converted to hexadecimal (base = 16) is 39F.
You can verify these values using a scientific calculator program available in windows OS.