Hi all, Im trying to write a program to convert between bases. I have written a function to convert base 10 numbers to any base, but i'm alittle confuse about doing the opposite. (converting any base to base 10) Can someone help me with the code to do so. I'd appreciate it

# Help with Converting any base to base 10

Page 1 of 1## 10 Replies - 11764 Views - Last Post: 14 April 2007 - 10:03 AM

##
**Replies To:** Help with Converting any base to base 10

### #2

## Re: Help with Converting any base to base 10

Posted 10 April 2007 - 09:59 AM

Wel dude i got ur question,

I did not try execution this the logic is as follows,

Just lemme know Ur progress with this

I did not try execution this the logic is as follows,

take i=base initialize ur converted no to a/10;(where a = give number to base i) for(i=base;(a/10)!=0;i*=i) { sum+=sum*i; }

Just lemme know Ur progress with this

### #3

## Re: Help with Converting any base to base 10

Posted 10 April 2007 - 02:39 PM

hey, thanks for trying to help. But i'm still confuse as how to implement it. Thats my fault. here is what i have so far.

this will give me the binary of a base 10. Simple enough, but i want to enter a number in whatever base (between 2-10) and then convert it into binary. I think it might help if i can convert that number to base 10 first. I just dont know how to code the math. please help. thank you

int base2_conversion(int n); int main() { int n; do{ cout << "Enter a number: "; cin >> n; cout << "Enter a base: "; cin >> b; cout << n << " in base 2 = "; base2_conversion(n); cout << endl; }while (n!=0); return 0; } //------------------------------------------------------------------------- //------------------------------------------------------------------------ int base2_conversion(int n) { int num = n; // number to convert int base = b; // base to convert number to. Convert the number to base 2 while (num) { cout << (num % base); // print the remainder of division Num / base then, num /= base; // do the division and print the integer } return num; }

this will give me the binary of a base 10. Simple enough, but i want to enter a number in whatever base (between 2-10) and then convert it into binary. I think it might help if i can convert that number to base 10 first. I just dont know how to code the math. please help. thank you

### #4

## Re: Help with Converting any base to base 10

Posted 10 April 2007 - 09:36 PM

Well dude so here it is for U

Do leave coments.[]

int conversion(int a,int base) { int ans=a%10; for(int i=base;(a/=10)!=0;i*=base) sum+=a*i; return ans; } int main() { int a,base,ans; cout << "Enter a number: "; cin >> a; cout << "Enter a base: "; cin >> base; ans=convert(a,base); cout << "the converted base 10 is"<<ans; return 0; }

Do leave coments.[]

### #5

## Re: Help with Converting any base to base 10

Posted 10 April 2007 - 10:27 PM

### #6

## Re: Help with Converting any base to base 10

Posted 11 April 2007 - 09:21 AM

harshakirans, on 10 Apr, 2007 - 09:36 PM, said:

Well dude so here it is for U

Do leave coments.[]

int conversion(int a,int base) { int ans=a%10; for(int i=base;(a/=10)!=0;i*=base) sum+=a*i; return ans; } int main() { int a,base,ans; cout << "Enter a number: "; cin >> a; cout << "Enter a base: "; cin >> base; ans=convert(a,base); cout << "the converted base 10 is"<<ans; return 0; }

Do leave coments.[]

Hi, thanks for the help, but i tried your code, but it didnt return the correct base 10 equivalent.

### #7

## Re: Help with Converting any base to base 10

Posted 11 April 2007 - 09:42 AM

Hey i ve tried this on TC compiler it works fine And post me the error or warning if any encountered or the special input if in case......

#include<iostream.h> #include<conio.h> int convert(int a,int base) { int sum=a%10; for(int i=base;(a/=10)!=0;i*=base) sum+=a*i; return sum; } int main() { int a,base,ans; cout << "Enter a number: "; cin >> a; cout << "Enter a base: "; cin >> base; ans=convert(a,base); cout << "the converted base 10 is"<<ans; getch(); return 0; }

### #8

## Re: Help with Converting any base to base 10

Posted 11 April 2007 - 10:06 AM

Quote

int conversion(int a,int base) { int ans=a%10; for(int i=base;(a/=10)!=0;i*=base) sum+=a*i; return ans; }

LOL!!!

Hey the logic is almost right.

The computer stores integers in binary (in fact it stores everything in binary). When you call cout or printf to output a integer it converts the integer (base 2) into a string repersenting the number in base 10...

the problem with the above program is that: NumberBase2 = NumberBase10 = NumberBase16 in the computer because they ALL get converted to base 2.

of course the above program should not even compile as sum is not declared, and it should be ans anway... whatever, the logic from converting bases is there.

If you want to use the integer format to hold non-base 10 numbers, and use cout or printf to print them then the base must be LESS than 10. So for example a base 8 number 751 can be converted using only integer operations. But you can't use this technique to store base 16 numbers.

Suppose that I had an integer held in the Num, and I wanted to find its Lease significat digit... that would be Num%10. Then if I shift the digest to the right 1 place (division by 10... Num /= 10) I can repeat the Num%10 to get the next digit... Now I CAN'T just say ans = ans + (Num%10) * pow(10, i) since when I am done ans will be equal to what Num stated out as... rather stupid as I could have just said: ans=num; and been done with it. Instead I must store the number in a string or another format which seperates the digits.

Lets say I wanted to conver a base 8 (base less than 10) number stored in an integer into a regular integer... so like 751 = 489... to do this I need to do like the above program and treat the number like it is base 10, to extract the digits, but to once I do, I want to use ans=ans+(Num%10)*pow(8,i) that is, I need to remeber that each "digit" is a power of 8, not 10.

### #9

## Re: Help with Converting any base to base 10

Posted 13 April 2007 - 12:00 PM

Quote

Suppose that I had an integer held in the Num, and I wanted to find its Lease significat digit... that would be Num%10. Then if I shift the digest to the right 1 place (division by 10... Num /= 10) I can repeat the Num%10 to get the next digit... Now I CAN'T just say ans = ans + (Num%10) * pow(10, i) since when I am done ans will be equal to what Num stated out as... rather stupid as I could have just said: ans=num; and been done with it. Instead I must store the number in a string or another format which seperates the digits.

Lets say I wanted to conver a base 8 (base less than 10) number stored in an integer into a regular integer... so like 751 = 489... to do this I need to do like the above program and treat the number like it is base 10, to extract the digits, but to once I do, I want to use ans=ans+(Num%10)*pow(8,i) that is, I need to remeber that each "digit" is a power of 8, not 10.

Lets say I wanted to conver a base 8 (base less than 10) number stored in an integer into a regular integer... so like 751 = 489... to do this I need to do like the above program and treat the number like it is base 10, to extract the digits, but to once I do, I want to use ans=ans+(Num%10)*pow(8,i) that is, I need to remeber that each "digit" is a power of 8, not 10.

Wel first thanks for that reply,

I agree that this doesnt fetch the output when the base is more than 10,but i did not get your later explanation

Quote

Now I CAN'T just say ans = ans + (Num%10) * pow(10, i) since when I am done ans will be equal to what Num stated out as... rather stupid as I could have just said: ans=num;

There is no way sum will be equal to num.......

Would u plz be clear of the problem u stated and also the betterment U suggested.

Thanks again for that post...

### #10

## Re: Help with Converting any base to base 10

Posted 13 April 2007 - 01:47 PM

Well the part you quoted there (ans = ans + (Num%10) * pow(10, i)) is a little fishy since I was misreading your code when I wrote it. Basicly it is just saying that conversion form base 10 to base 10 is rather useless. I wrote this because

Your code is rather nice and works well. The only problem with it was my eyes.

**I had mistakenly read**your code as for(int i=base;(a/=10)!=0;i*=10) which ofcourse is not what it was doing.Your code is rather nice and works well. The only problem with it was my eyes.

### #11

## Re: Help with Converting any base to base 10

Posted 14 April 2007 - 10:03 AM

Hey, thanks alot for trying to help me out you guys. I thought about it for awhile and i found the solution to my problem.

Page 1 of 1