Paste: base changer

Author: Rex Ford
Mode: c++
Date: Fri, 10 Feb 2012 06:34:18
Plain Text |
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int base=37;
	int num;
	string symbols = "0123456789abcdefghijklmnopqrstuvwxyz";
	
	cout<<"What number?\n";
	cin >> num;
	while(base>36 || base<2)
	{
		cout<<"What base?\n";
		cin >> base;
		if(base>36 || base<2)
			cout<<"Your chosen base was not from 2-36...\n";
	}
	
	//calculates the numbers of digits that num will have when represented in the new base
	int num_digits=(int)1+(log((double)num)/log((double)base));

	for(int exp=num_digits-1;exp>=0;exp--)
	{
		int digit_value = num/pow(base,exp);
		cout<<symbols.at(digit_value);
		num=num-digit_value*pow(base,exp);
	}
	cout<<endl;
}

New Annotation

Summary:
Author:
Mode:
Body: