•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,469 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1279 | Replies: 10 | Solved
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> int toAnyBase(int n, int toBase); // from base 10 (2nd) int toBaseTen(char array[256], int fromBase); // from any base (1st) int fromAnyToAny(char input[],int fromBase,int toBase); int main() { char input[10]; int fromBase=0; int toBase=0; int answer=0; int answer2=0; printf("Enter number :"); fgets(input, sizeof(input),stdin); printf("\n Enter base of input: "); scanf("%d",&fromBase); printf("\n Enter base of output: "); scanf("%d",&toBase); answer=fromAnyToAny(input,fromBase,toBase); system("pause"); return 0; } int fromAnyToAny(char input[],int fromBase,int toBase) { int x=0; int y=0; x=toBaseTen(input,fromBase); y=toAnyBase(x,toBase); return y; printf("%d", y); } int toAnyBase(int n, int toBase) { int counter=0; int bin[256]; int quo=1; int answer; while(quo!=0) { quo=n/toBase; bin[counter]= n % toBase; counter++; n=quo; } while(counter>0) { printf("%d",bin[counter-1]); counter--; } printf("\n"); return answer; } int toBaseTen(char array[256], int fromBase) { int i,j; int arraylen; int n; int sum=0; int pwr; arraylen=strlen(array)- 1; for(i=0,j=arraylen-1;i<arraylen,j>-1;i++,j--) { n = (array[i] - '0'); sum+= n*pow(fromBase,j); } return sum; }
I am having trouble converting from a base10 number to any other base between 2 and 10. For example if i convert 255 from base 10 to base 2 i am getting 11111110 instead of 11111111. Can someone please help?
Last edited by imran_s : Nov 8th, 2007 at 10:57 am.
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Well i am new to C programming. When i use the tweo functions "int toBaseTen(char array[256], int fromBase)" and "int toAnyBase(int n, int toBase)" as two separate programs i am getting the desired output. But its not working when its as one unit. If i try converting from any base (betweeen 2-10) to base 10 it works fine. Example 513 from base 7 to base 10 is 255.
>Example: If i convert a base 10 number (255) to base 2 i get 11111110;
I repeat. My testing does not show this behavior. If I run your program (exactly as posted), type 255 for the value, 10 for the input base, and 2 for the output base, the result is printed as 11111111. Your example is faulty, or your description of how to reproduce the program is faulty.
I repeat. My testing does not show this behavior. If I run your program (exactly as posted), type 255 for the value, 10 for the input base, and 2 for the output base, the result is printed as 11111111. Your example is faulty, or your description of how to reproduce the program is faulty.
I'm here to prove you wrong.
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
>Example: If i convert a base 10 number (255) to base 2 i get 11111110;
I repeat. My testing does not show this behavior. If I run your program (exactly as posted), type 255 for the value, 10 for the input base, and 2 for the output base, the result is printed as 11111111. Your example is faulty, or your description of how to reproduce the program is faulty.
I did the same thing that you did on more than 1 computer and i get the same problem. Lets say u wanna convert 255 from base 10 to base 10. Youre supposed to get 255 as the answer, howeever i am getting 254. when i try to convert any number from 100 up from base ten to any other base i am getting the answer as 1 short the correct answer.
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,829
Reputation:
Rep Power: 11
Solved Threads: 193
•
•
•
•
I did the same thing that you did on more than 1 computer and i get the same problem. Lets say u wanna convert 255 from base 10 to base 10. Youre supposed to get 255 as the answer, howeever i am getting 254. when i try to convert any number from 100 up from base ten to any other base i am getting the answer as 1 short the correct answer.
No such problem here. I have to agree with Narue, your code works (more or less) for me. There are a few other thing wrong with it though: Always initialize your vars with a value! Always check if your input is valid! etc
What compiler and OS are you using? I tested it with VS2005 on WinXP
Regards Niek
Last edited by niek_e : Nov 9th, 2007 at 4:47 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
do NOT pm me for help, in the best case, you'll get ignored
•
•
•
•
I did the same thing that you did on more than 1 computer and i get the same problem. Lets say u wanna convert 255 from base 10 to base 10. Youre supposed to get 255 as the answer, howeever i am getting 254. when i try to convert any number from 100 up from base ten to any other base i am getting the answer as 1 short the correct answer.
The problem is in the way your compiler deals with this particular statement:
sum+= n*pow(fromBase,j);
A couple of simple printfs will show the case:
C Syntax (Toggle Plain Text)
int toBaseTen(char array[256], int fromBase) { int i,j; int arraylen; int n; int sum=0; arraylen=strlen(array)- 1; for(i=0,j=arraylen-1;i<arraylen,j>-1;i++,j--) { n = (array[i] - '0'); printf( "n = %d; n*pow( fromBase, j ) = %.2f\n", n, ( n*pow(fromBase, j)) ); sum+= n*pow(fromBase,j); printf( "sum = %d\n", sum ); } return sum; } /* input/output: Enter number :255 Enter base of input: 10 Enter base of output: 2 n = 2 n*pow( fromBase, j ) = 200.00 sum = 199 n = 5 n*pow( fromBase, j ) = 50.00 sum = 249 n = 5 n*pow( fromBase, j ) = 5.00 sum = 254 11111110 Press any key to continue . . . */
Last edited by Aia : Nov 10th, 2007 at 11:42 pm.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Base Conversion (Java)
- Need advice and base conversion. Please help as soon as you can. Thanks (C++)
- Assembler n00b. Need Help Please. (Assembly)
- Convert decimal to binary and with base the 8 (C)
- base conversion (Java)
- converting base class to dervived class (C)
- Base converter (C++)
- Copy constructor in derived class (C)
- the books you use (Computer Science and Software Design)
Other Threads in the C Forum
- Previous Thread: arrays
- Next Thread: COMport C++ problem



Hybrid Mode