I dispair if your teacher did teach you to do it that way....
1110101 = 117....not 85..
Code:
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
void ToBin(int x){
const int mask = 1 << ((8 * sizeof(int))-1);
while(x){
cout << ((mask & x)?'1':'0');
x <<= 1;
}
cout << endl;
}
void ToDec(char* y){
int mult = 1,
total = 0;
int len = strlen(y);
char* c = y + len - 1;
for(int i = 0;i<len;i++,c--){
if((char)*c == '1')
total += 1*mult;
mult *= 2;
}
cout << total << endl;
}
int main( void )
{
ToBin(85);
ToDec("1010101");
return 0;
}
Here's one quick way of converting to and fro....