### 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:
 ``12345678910111213141516`` ``````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?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116`` ``````#include #include #include #include #include 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"< Nonbase-10"< Base-10"< Nonbase-10"<>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<>non10In; cout<<"To what base does this belong? "; cin>>bIn; b10Num = toBase10(non10In, bIn); cout<>non10In; cout<<"To what base does this belong? "; cin>>bIn; cout<<"To which base should it be converted? "; cin>>bOut; non10Out = "Trying"; cout<

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)`

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
 ``1234567`` ``````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

 ``12345678910111213`` ``````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'?
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.

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` ``````#include #include #include #include #include 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"< Nonbase-10"< Base-10"< Nonbase-10"<>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<>non10In; cout<<"To what base does this belong? "; cin>>bIn; b10Num = toBase10(non10In, bIn); cout<>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<
see line 19 it should be `if (digit>= '0' ||digit<= '9' )`
Topic archived. No new replies allowed.