Skip to content

Instantly share code, notes, and snippets.

@ButchDean
Created November 17, 2013 04:52
Show Gist options
  • Save ButchDean/7509413 to your computer and use it in GitHub Desktop.
Save ButchDean/7509413 to your computer and use it in GitHub Desktop.
This piece of code makes use of the STL vector and map to convert a number from base 10 to any other base in the range 2-36. This code demonstrates the power of the STL in defining quite complicated structures efficiently, as well as saving on many lines of extra code in making use of such structures.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class CBaseConverter
{
public:
CBaseConverter(){}
CBaseConverter(int base) : bcBase(base)
{
if(bcBase < minBase || bcBase > maxBase)
validBase = false;
else
validBase = true;
}
~CBaseConverter(){}
bool NotifyIfInvalidBase()
{
if(!validBase)
{
cout << "Invalid base (outside range 2-36) " << bcBase << ".\n";
return validBase;
}
return validBase;
}
void ConstructSymbols();
void ConvertToBase(int decimalNum);
void PrintBaseConversion();
private:
int bcBase;
bool validBase;
static const int minBase = 2;
static const int maxBase = 36;
static const int digitStartInASCII = 0x30; // '0'
static const int alphaStartInASCII = 0x61; // 'a'
// Reference vector to pass proper symbol to bcBaseNVector.
// examples: map<0, '0'> and map<10, 'a'>.
map<int, char>baseSymbols;
// Stores the number in its new base.
vector<char>bcBaseNVector;
vector<char>::reverse_iterator baseIter;
};
void CBaseConverter::ConstructSymbols()
{
int i;
char firstChar = static_cast<char>(digitStartInASCII);
// Only fill baseSymbols with symbols that are going to be used
// for conversion.
cout << "\nSymbols generated: ";
for(i = 0; i < 10 && i < bcBase; i++)
{
baseSymbols[i] = firstChar + i;
cout << baseSymbols[i] << ' ';
}
firstChar = static_cast<char>(alphaStartInASCII);
for(i = 10; i < 36 && i < bcBase; i++)
{
baseSymbols[i] = firstChar + (i - 10);
cout << baseSymbols[i] << ' ';
}
cout << '\n';
}
void CBaseConverter::ConvertToBase(int decimalNum)
{
if(!NotifyIfInvalidBase())
return;
ConstructSymbols();
cout << "Converting " << decimalNum << " to base " << bcBase << ".\n";
do // Base conversion logic.
{
bcBaseNVector.push_back(baseSymbols[decimalNum % bcBase]);
decimalNum /= bcBase;
}
while(decimalNum);
}
void CBaseConverter::PrintBaseConversion()
{
if(!NotifyIfInvalidBase())
return;
cout << "Base " << bcBase << " conversion ";
for(baseIter = bcBaseNVector.rbegin(); baseIter != bcBaseNVector.rend(); ++baseIter)
cout << *baseIter;
cout << '\n';
}
int main()
{
CBaseConverter binary = CBaseConverter(2) ;
CBaseConverter octal = CBaseConverter(8) ;
CBaseConverter hex = CBaseConverter(16) ;
CBaseConverter base36 = CBaseConverter(36) ;
CBaseConverter invalidBase = CBaseConverter(360) ;
binary.ConvertToBase(20);
binary.PrintBaseConversion();
octal.ConvertToBase(80);
octal.PrintBaseConversion();
hex.ConvertToBase(160);
hex.PrintBaseConversion();
base36.ConvertToBase(392);
base36.PrintBaseConversion();
// Following omly notified of invalid base.
invalidBase.ConvertToBase(372);
invalidBase.PrintBaseConversion();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment