User Name Password Register
DaniWeb IT Discussion Community
All
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,611 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,430 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: 1281 | Replies: 10 | Solved
Reply
Join Date: Nov 2007
Posts: 5
Reputation: imran_s is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imran_s imran_s is offline Offline
Newbie Poster

Question Converting Any Base to Any Base

  #1  
Nov 8th, 2007
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int toAnyBase(int n, int toBase); // from base 10 (2nd)
  7. int toBaseTen(char array[256], int fromBase); // from any base (1st)
  8. int fromAnyToAny(char input[],int fromBase,int toBase);
  9.  
  10. int main()
  11. {
  12. char input[10];
  13. int fromBase=0;
  14. int toBase=0;
  15. int answer=0;
  16. int answer2=0;
  17.  
  18.  
  19. printf("Enter number :");
  20. fgets(input, sizeof(input),stdin);
  21. printf("\n Enter base of input: ");
  22. scanf("%d",&fromBase);
  23. printf("\n Enter base of output: ");
  24. scanf("%d",&toBase);
  25. answer=fromAnyToAny(input,fromBase,toBase);
  26. system("pause");
  27. return 0;
  28. }
  29.  
  30.  
  31. int fromAnyToAny(char input[],int fromBase,int toBase)
  32. {
  33. int x=0;
  34. int y=0;
  35. x=toBaseTen(input,fromBase);
  36. y=toAnyBase(x,toBase);
  37. return y;
  38. printf("%d", y);
  39. }
  40.  
  41. int toAnyBase(int n, int toBase)
  42. {
  43. int counter=0;
  44. int bin[256];
  45. int quo=1;
  46. int answer;
  47.  
  48.  
  49. while(quo!=0)
  50. {
  51. quo=n/toBase;
  52. bin[counter]= n % toBase;
  53. counter++;
  54. n=quo;
  55. }
  56.  
  57. while(counter>0)
  58. {
  59. printf("%d",bin[counter-1]);
  60. counter--;
  61. }
  62. printf("\n");
  63. return answer;
  64. }
  65.  
  66.  
  67. int toBaseTen(char array[256], int fromBase)
  68. {
  69. int i,j;
  70. int arraylen;
  71. int n;
  72. int sum=0;
  73. int pwr;
  74.  
  75. arraylen=strlen(array)- 1;
  76. for(i=0,j=arraylen-1;i<arraylen,j>-1;i++,j--)
  77. {
  78. n = (array[i] - '0');
  79. sum+= n*pow(fromBase,j);
  80. }
  81. return sum;
  82. }

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Converting Any Base to Any Base

  #2  
Nov 8th, 2007
>For example if i convert 255 from base 10 to base 2
>i am getting 11111110 instead of 11111111.
That particular example works fine for me. Though your code has some...interesting aspects.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Nov 2007
Posts: 5
Reputation: imran_s is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imran_s imran_s is offline Offline
Newbie Poster

Re: Converting Any Base to Any Base

  #3  
Nov 8th, 2007
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Converting Any Base to Any Base

  #4  
Nov 8th, 2007
So...when doesn't it work? So far you've given me one example that doesn't work for you but does work for me, and one example that works for you. An example that doesn't work for both of us would expedite the troubleshooting process just a smidge.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Nov 2007
Posts: 5
Reputation: imran_s is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imran_s imran_s is offline Offline
Newbie Poster

Re: Converting Any Base to Any Base

  #5  
Nov 8th, 2007
Ok. its working but giving errenuous results.
Example: If i convert a base 10 number (255) to base 2 i get 11111110;
When i convert the result 11111110 back to base 10 i get 254.

i should get 11111111 from the first conversion and not 11111110
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Converting Any Base to Any Base

  #6  
Nov 8th, 2007
>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'm here to prove you wrong.
Reply With Quote  
Join Date: Nov 2007
Posts: 5
Reputation: imran_s is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imran_s imran_s is offline Offline
Newbie Poster

Re: Converting Any Base to Any Base

  #7  
Nov 8th, 2007
Originally Posted by Narue View Post
>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.
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,829
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 193
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Converting Any Base to Any Base

  #8  
Nov 9th, 2007
Originally Posted by imran_s View Post
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
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Converting Any Base to Any Base

  #9  
Nov 10th, 2007
Originally Posted by imran_s View Post
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);
You are loosing value. sum is an integer, but the result of the multiplication of n and pow() is a float. The conversion to integer looses some value.
A couple of simple printfs will show the case:
  1. int toBaseTen(char array[256], int fromBase)
  2. {
  3. int i,j;
  4. int arraylen;
  5. int n;
  6. int sum=0;
  7.  
  8. arraylen=strlen(array)- 1;
  9.  
  10. for(i=0,j=arraylen-1;i<arraylen,j>-1;i++,j--)
  11. {
  12. n = (array[i] - '0');
  13. printf( "n = %d; n*pow( fromBase, j ) = %.2f\n", n, ( n*pow(fromBase, j)) );
  14. sum+= n*pow(fromBase,j);
  15. printf( "sum = %d\n", sum );
  16.  
  17. }
  18. return sum;
  19. }
  20. /* input/output:
  21. Enter number :255
  22. Enter base of input: 10
  23. Enter base of output: 2
  24.  
  25. n = 2 n*pow( fromBase, j ) = 200.00
  26. sum = 199
  27. n = 5 n*pow( fromBase, j ) = 50.00
  28. sum = 249
  29. n = 5 n*pow( fromBase, j ) = 5.00
  30. sum = 254
  31. 11111110
  32. Press any key to continue . . .
  33. */
As you see the first sum should have shown a value of 200 as expected, however it holds only a 199 value carrying over all the way down a deficit of 1.
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 5
Reputation: imran_s is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
imran_s imran_s is offline Offline
Newbie Poster

Re: Converting Any Base to Any Base

  #10  
Nov 11th, 2007
Thanks Aia...i realize if i declare sum as of type double instead of integer i get the correct value.
Thanks everybody
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 7:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC