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; }
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.