Base conversion so confused ? help?

THis is my code so far how can i do base conversion to any bases between 2 - 16. I don't really know how.
Could i use this
long int strtol(const char* nptr, char** endptr, int base);
sprintf(str,"%o",value)

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
if(argc=3)
{ int num1 = atoi(argv[1])
char* bVar = argv[2];
int bVal;
if (bVar[0] == 'b')
{
bVal = atoi(bVar + 1);
}
if( bVal < 2 || bVal > 16 )
{cout << "X" << endl; }
Last edited on
Can you do it with a pencil and paper?
An easy approach would be to convert the input number to binary and then convert the binary number to the output base.

Converting to binary requires finding the largest power of two less than the number, subtracting it off, putting a 1 in that binary digit, and repeating. Converting from binary is easier; just add up all the 1s times 2^n, where n is the number of the digit starting with 0 at the right.
yes i get
24 / 2 = 12 remainder 0
12 / 2 = 6 remainder 0
6 / 2 = 3 remainder 0
3 / 2 = 1 remainder 1
1 / 2 = 0 remainder 1

so you 1100

but through that this code sprintf(str,"%o",value), can i do it or is there another way using atop
Topic archived. No new replies allowed.