VB icon

Base10 Convertor

Email
Submitted on: 10/18/2016 11:16:32 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1927
 
     A small project that converts decimal numbers to 6 different bases. Hope it maybe usfull.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Base10 Convertor
// Description:A small project that converts decimal numbers to 6 different bases. Hope it maybe usfull.
// By: Ben
//**************************************

// Base convert
// By DreamVB 00:01 19/10/2016
#include <iostream>
using namespace std;
using std::cout;
using std::endl;
string BaseConvert(int decimal, int radix){
	//Base convertor
	char hexmap[16] = { '0', '1', '2', '3', '4',
		'5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	string Ret = "";
	int n = decimal;
	int base = 0;
	while (n >= radix){
		base = (n % radix);
		n /= radix;
		Ret += hexmap[base];
	}
	//Add last hexmap char
	Ret += hexmap[n];
	//Reverse string
	std::reverse(Ret.begin(), Ret.end());
	return Ret;
}
int main(int argc, char *argv[]){
	int dec = 0;
	system("title Base10 Converting");
	cout << "Enter a decimal number : ";
	cin >> dec;
	cout << endl;
	cout << "Binary : " << BaseConvert(dec, 2).c_str() << endl;
	cout << "Ternary : " << BaseConvert(dec, 3).c_str() << endl;
	cout << "Quinary : " << BaseConvert(dec, 5).c_str() << endl;
	cout << "Octonary: " << BaseConvert(dec, 8).c_str() << endl;
	cout << "Decimal : " << BaseConvert(dec, 10).c_str() << endl;
	cout << "Hexidecimal : " << BaseConvert(dec, 16).c_str() << endl;
	system("pause");
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.