Hi, I'm trying to write a program that converts a base x to a base y. It says that we are suppose to use two functions to do so: one that converts base x to base 10 and one that converts base 10 to base y but I don't understand the whole "base" concept thus don't even know where to start. Can someone help me out? Possibly give me an example or something?

Thanks. :)

Recommended Answers

All 4 Replies

Base means the longest sequence of fundamental digits before reaching 10. 10 is not limited to the decimal counting system, it is used in other bases to mean one more than the base:

Base 10, decimal: 0 1 2 3 4 5 6 7 8 9 10
Base 8, octal: 0 1 2 3 4 5 6 7 10
Base 16, hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F 10

When converting between bases, you are recounting to include new fundamental digits, or exclude missing ones. Say you want to convert hexadecimal 0x1B to decimal. Basically it goes like this:

0 = 0        A  = 10        14 = 20
1 = 1        B  = 11        15 = 21
2 = 2        C  = 12        16 = 22
3 = 3        D  = 13        17 = 23
4 = 4        E  = 14        18 = 24
5 = 5        F  = 15        19 = 25
6 = 6        10 = 16        1A = 26
7 = 7        11 = 17        1B = 27
8 = 8        12 = 18
9 = 9        13 = 19

So 1B in hexadecimal is 27 in decimal. Actually counting it out like that is probably the most naive algorithm for solving the problem, but that is the idea behind all of the more complex algorithms for base conversion. :)

I got this so far for converting base 10 to base y based on some research I was able to do online on how to convert base 10 to another base (although it wasn't in regards to C++). Am I on the right track?

// This program converts any number from base x to base y. It prompts the
// user for the number, base x and base y and then converts the base x to 
// base y.

#include "stdafx.h"
#include <iostream>
using namespace std;

void baseYConverter(int num2);

int main()
{
  // declare variables
  int num1, num2, num3;

  // get user number, base x and base y
  while (true)
  {
    cout << "Please enter the #, then base x and base y. (separate w/spaces): ";
    cin >> num1 >> num2 >> num3;

    if (num2 >= 2 && num2 <= 16)
    if (num3 >= 2 && num3 <= 16) break;
    cout << "Bases are from 2-16. Please try again." << endl;
  } // while

void baseYConverter(int num2);

	return 0;
}



// convert base 10 to base y
void baseYConverter(int num2)
{
	const int RANGE = 50;
	int array[RANGE];
	int i = 0;

	while(num2 != 0)
	{
		array[i] = (num2 % 2);
		num2 /= 2;
		i++;
	} // while

} // baseYConverter

Extremely new at programming but, what did you use for your header file? I guess I am wondering where the base y converter came from?

From his brain. He wrote it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.