Base Conversion – Handle upto 36 bases

Sometimes we need base conversion for different purpose. Here I have shown a technique that handles upto 36 bases. I implemented this using C++ language. But using this concept you could modify it and change it to any language.

The main concept is, to change base ‘X’ to base ‘Y’. First convert base ‘X’ to base ‘Decimal’. Then convert the ‘Decimal’ base to base ‘Y’.

/*BASE CONVERSION
Programmer: Md. Mahmud Ahsan
Description: Alpha Numeric Base Conversion
Handle upto 36 bases
*/
#include <iostream>
#include <string>
using namespace std;

int myPow(int number, int power){
int total=1;
for (int i = 0; i < power; ++i)
total *= number;
return total;
}

void revStr(char *str){
char s[100];
int len = strlen(str)-1;
for (int i = 0; str[i]; ++i)
s[i] = str[len-i];
s[i] = ”;
strcpy(str, s);
}

int otherToDec(char *str, int base){
int len = strlen(str) – 1;
int alpha[100];
long long i, number, j=9; // j for alphabetical base digit
for (i = 65; i < 65+26; ++i){
alpha[i] = ++j; // collect numeric data suppose A=10
}

number = 0;
for (i = 0; str[i]; ++i){
if (str[i] >= ‘A’ && str[i] <= ‘Z’)
j = alpha[str[i]];
else
j = str[i] – 48;
number += j * myPow(base, len);
len -=1;
}
return number;
}

char* decToOther(int number, int base){
char str[] = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
char final[100];
int n, temp, j;

j = -1;
do{
temp = number % base;
final[++j] = str[temp];
number = number / base;
}while (number != 0);

final[++j] = ”;
revStr(final);
return final;
}

int main(){
int number, base10, base, from, to;
char str[100];
while (cin >> str >> from >> to){
base10 = otherToDec(str, from);
strcpy(str, decToOther(base10 ,to));
cout << “Base 10: ” << base10 << ” ”
<< “Base ” << to << “: ” << str << endl;

}
return 0;
}

Advertisements
Posted in C / C++. Tags: , , . 2 Comments »

2 Responses to “Base Conversion – Handle upto 36 bases”

  1. download crysis 3 Says:

    First off I want to say awesome blog! I had a quick question in which I’d like to ask if you do not mind. I was curious to know how you center yourself and clear your head prior to writing. I’ve had
    trouble clearing my thoughts in getting my thoughts out there.
    I do enjoy writing however it just seems like the first 10 to 15 minutes are generally wasted just trying
    to figure out how to begin. Any ideas or hints? Kudos!

  2. Watch Percy Jackson Sea Of Monsters Online Free Says:

    I really like what you guys are up too. This sort of clever work
    and reporting! Keep up the very good works
    guys I’ve incorporated you guys to my blogroll.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: