Convert from decimal to base-n number system
We can implement this with a simple, easy to understand program:
void g( int x, int n ) { if ( x ) { g( x / n, n ); std::cout << x % n; } }
Convert from decimal to base-n number system
Let’s consider an example converting a binary number to decimal number:
We also can write it like this:
Expanding for base-n number system, if we have a number in base-n number system:
Then:
Finally we have a program without the need of pow function:
int h( char * src, int n ) { int decimal = 0; for( char * p = src; p - src < strlen( src ); p++ ) { decimal = decimal * n + *p - '0'; } return decimal; }