Number conversion

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;
}


Where am I going wrong?
what does digitToValue(nB10[i]) do

do you realise line 8 int step=pow(base,j) will always be 1
and j++ in line 13 is not being used
Last edited on
But I incremented j at the bottom, so for the next cycle of the loop, j should be 1, right?
And then my idiot self didn't read your second line of text. Sorry!
How can I fix it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>

using namespace std;

int digitToValue (char digit)
{
   if (digit>=0||digit<=9)
      return digit-48;
   else
      return digit-55;
}

int valueToDigit (int digit)
{
   if (digit>=0||digit<=9)
      return digit+48;
   else
      return 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.
I hadn't made it to fixing the third part, yet. It needs the second to work right.
as far as i know the logic is
if you have an octal no (123)8 then
1*pow(8,2)+2*pow(8,1)+3*pow(8,0)

should give you the answer
But I have to write that in a loop so the rest of the program can use it.
i don't get what this does
1
2
3
4
5
6
7
int digitToValue (char digit)
{
   if (digit>=0||digit<=9)
      return digit-48;
   else
      return digit-55;
}
It takes a character and turns it into an integer.
It's the reverse of valueToDigit
see and implement from this

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    using namespace std;

    string one="123";
    int base=8,sum=0;
    unsigned int len=one.length()-1;
    for(unsigned int i=0; i<=len ; i++)
    {
        sum+=(one[len-i]-'0')*pow(base,i);
    }
    cout << "(123)8="<<'('<< sum <<")10";
}
Why the [len-i]-'0'? Why -'0'?
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;
   else
      return digit-55;
}

char valueToDigit (int digit)
{
   if (digit>=0||digit<=9)
      return digit+48;
   else
      return digit+55;
}
Okay, so they were supposed to be &&, not ||. Is there anything else I'm missing?
if (digit>=0||digit<=9) is not same as if (digit>= '0' ||digit<= '9' )

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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>

using namespace std;

int digitToValue (char digit)
{
   if (digit>='0'&&digit<='9')
      return digit-48;
   else
      return digit-55;
}

char valueToDigit (int digit)
{
   if (digit>=0&&digit<=9)
      return digit+48;
   else
      return 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;
}
see line 19 it should be if (digit>= '0' ||digit<= '9' )
Topic archived. No new replies allowed.