I made a post yesterday about me trying to convert numbers between bases, but I think it got passed over because of a few replies that were more questions. Either way, I think I have most of it, but I can't get one part right. I'm trying to convert numbers from a base other than ten, to a decimal base. Here is the module I'm using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
long toBase10 (string nB10, long base)
{
int j=0;
long sum=0;
int temp;
int leng=nB10.length();
int i;
int step=pow(base,j);
for (i=leng; i>=0; i--)
{
temp=digitToValue(nB10[i]);
sum+=(temp*step);
j++;
}
return sum;
}
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
usingnamespace std;
int digitToValue (char digit)
{
if (digit>=0||digit<=9)
return digit-48;
elsereturn digit-55;
}
int valueToDigit (int digit)
{
if (digit>=0||digit<=9)
return digit+48;
elsereturn digit+55;
}
string fromBase10 (long base10Num, long base)
{
int Q=base10Num;
int b=base;
int r;
string ans="";
string str="";
while (Q!=0)
{
r=Q%b;
str=valueToDigit(r);
Q=Q/b;
ans=str+ans;
}
return ans;
}
long toBase10 (string nB10, long base)
{
int j=0;
long sum=0;
int temp;
int leng=nB10.length();
int i;
for (i=leng; i>=0; i--)
{
for (j=0; j<=leng; j++)
{
long step1=pow(base,j);
long step2=temp*step1;
temp=digitToValue(nB10[i]);
sum=sum+step2;
}
}
return sum;
}
void menu()
{
cout<<" Number System Application"<<endl;
cout<<"================================="<<endl;
cout<<"[1] Base-10 -> Nonbase-10"<<endl;
cout<<"[2] Nonbase-10 -> Base-10"<<endl;
cout<<"[3] Nonbase-10 -> Nonbase-10"<<endl;
cout<<"[0] Exit Program"<<endl;
}
int main()
{
int option;
do{
menu();
string non10In, non10Out;
long convTo, b10Num;
long bIn, bOut;
cout<<endl;
cout<<"Select an option: ";
cin>>option;
switch(option)
{
case 1: cout<<"Enter a non-negative integer: ";
cin>>b10Num;
cout<<"To which base should it be converted? ";
cin>>convTo;
non10Out = fromBase10(b10Num, convTo);
cout<<b10Num<<" = "<<non10Out<<" base "<<convTo<<endl;
break;
case 2: cout<<"Enter a non base-10 number: ";
cin>>non10In;
cout<<"To what base does this belong? ";
cin>>bIn;
b10Num = toBase10(non10In, bIn);
cout<<non10In<<" = "<<b10Num<<"base 10"<<endl;
break;
case 3: cout<<"Enter a non base-10 number: ";
cin>>non10In;
cout<<"To what base does this belong? ";
cin>>bIn;
cout<<"To which base should it be converted? ";
cin>>bOut;
non10Out = "Trying";
cout<<non10In<<" = "<<non10Out<<" base "<<bOut<<endl;
break;
case 0:
break;
default:
cout<<"Invalid option. Please try again."<<endl;
}
}while(option!=0);
return 0;
}
Messier than I'd wanted, but you probably know better what to do with it.
look at this page http://www.asciitable.com/
ascii value of '0' is 48 '1' is 49.... i am subtracting all the input ascii characters by '0' to get it in decimal same as your int digitToValue (char digit)
and i dont think your int digitToValue (char digit) will work properly think about it
Alright, this is what I have for the digitToValue and valueToDigit. Do they look right? I've been staring at them for so long I don't even know anymore.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int digitToValue (char digit)
{
if (digit>=0||digit<=9)
return digit-48;
elsereturn digit-55;
}
char valueToDigit (int digit)
{
if (digit>=0||digit<=9)
return digit+48;
elsereturn digit+55;
}
You're right, I'm sorry. I probably seem like a stubborn idiot, and I apologize. I've been working on this for 8 hours now, and I'm getting worn out. But it's due tonight. This is what I have so far. Is there any other places you notice? Every time I try to pull case 2, I enter in "2011" with the base "8". It's giving me "8216".
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
usingnamespace std;
int digitToValue (char digit)
{
if (digit>='0'&&digit<='9')
return digit-48;
elsereturn digit-55;
}
char valueToDigit (int digit)
{
if (digit>=0&&digit<=9)
return digit+48;
elsereturn digit+55;
}
string fromBase10 (long base10Num, long base)
{
int Q=base10Num;
int b=base;
int r;
string ans="";
string str="";
while (Q!=0)
{
r=Q%b;
str=valueToDigit(r);
Q=Q/b;
ans=str+ans;
}
return ans;
}
long toBase10 (string nB10, long base)
{
int j=0;
long sum=0;
int temp;
int leng=nB10.length();
int i;
for (i=0; i<=leng; i++)
{
sum+=(nB10[leng-i]-'0')*pow(base,i);
}
return sum;
}
void menu()
{
cout<<" Number System Application"<<endl;
cout<<"================================="<<endl;
cout<<"[1] Base-10 -> Nonbase-10"<<endl;
cout<<"[2] Nonbase-10 -> Base-10"<<endl;
cout<<"[3] Nonbase-10 -> Nonbase-10"<<endl;
cout<<"[0] Exit Program"<<endl;
}
int main()
{
int option;
do{
menu();
string non10In, non10Out;
long convTo, b10Num, n10I;
long bIn, bOut;
cout<<endl;
cout<<"Select an option: ";
cin>>option;
switch(option)
{
case 1: cout<<"Enter a non-negative integer: ";
cin>>b10Num;
cout<<"To which base should it be converted? ";
cin>>convTo;
non10Out = fromBase10(b10Num, convTo);
cout<<b10Num<<" = "<<non10Out<<" base "<<convTo<<endl;
break;
case 2: cout<<"Enter a non base-10 number: ";
cin>>non10In;
cout<<"To what base does this belong? ";
cin>>bIn;
b10Num = toBase10(non10In, bIn);
cout<<non10In<<" = "<<b10Num<<" base 10"<<endl;
break;
case 3: cout<<"Enter a non base-10 number: ";
cin>>non10In;
cout<<"To what base does this belong? ";
cin>>bIn;
cout<<"To which base should it be converted? ";
cin>>bOut;
n10I = toBase10(non10In, bIn);
non10Out= fromBase10(n10I, bOut);
cout<<non10In<<" = "<<non10Out<<" base "<<bOut<<endl;
break;
case 0:
break;
default:
cout<<"Invalid option. Please try again."<<endl;
}
}while(option!=0);
return 0;
}