Convert Decimal to Radix

Thread Solved

Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Convert Decimal to Radix

 
0
  #11
Sep 17th, 2009
result[numchars++] = basechars[r];

When you're done, print from numchars-1 back down to 0 (one char at a time).
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #12
Sep 17th, 2009
when I add the result[i++] the program crashes, I think because it keeps incrementing infinity
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Convert Decimal to Radix

 
3
  #13
Sep 17th, 2009
Post your code!
Did you initialise i ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #14
Sep 17th, 2009
  1. #include <stdio.h>
  2. #define SIZE 25
  3. int main(void) {
  4. int d, b;
  5.  
  6.  
  7. printf("Enter decimal and base: ");
  8. scanf("%d", &d);
  9. scanf("%d", &b);
  10.  
  11. if(b<2){
  12. printf("Your base is too low! \n");
  13. }
  14. else if (b >16){
  15. printf("Your base is too high! \n");
  16. }
  17. else if(d==0){
  18. printf("%d \n", 0);
  19. }
  20. else{
  21.  
  22. while (d!=0){
  23. char temp[SIZE];
  24. char result[SIZE];
  25. int i, r, n ;
  26. r = d%b;
  27. d = d/b;
  28. char basechars[] = "0123456789ABCDEF";
  29. temp[i++] = basechars[r];
  30. result[n++] = temp[--i];
  31.  
  32. printf("%c", result);
  33. }
  34. }
  35. system("PAUSE");
  36.  
  37. }
Last edited by Kombat; Sep 17th, 2009 at 6:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: Convert Decimal to Radix

 
0
  #15
Sep 17th, 2009
You're making this more difficult than it needs to be. Look at this code snippet:
  1. while (d != 0) {
  2. r = d % b;
  3. d = d / b;
  4. result[len++] = basechars[r];
  5. }
  6.  
  7. for (i = len-1; i >= 0; --i) {
  8. printf("%c", result[i]);
  9. }
Last edited by yellowSnow; Sep 17th, 2009 at 8:26 pm.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #16
Sep 17th, 2009
Wow thanks it works however it is showing a lot of weird symbols along with the answer.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: Convert Decimal to Radix

 
0
  #17
Sep 17th, 2009
Originally Posted by Kombat View Post
Wow thanks it works however it is showing a lot of weird symbols along with the answer.
Post your full code.
And don't thank me - Salem is the one you should be thanking.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #18
Sep 17th, 2009
I pmed you it
Last edited by Kombat; Sep 17th, 2009 at 8:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: Convert Decimal to Radix

 
1
  #19
Sep 17th, 2009
Initialize k to zero.
Also, move your declarations for r and basechars outside of the loop.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Convert Decimal to Radix

 
0
  #20
Sep 17th, 2009
the moving r and basechars made it fail but initializing the k to zero did the trick.

Thanks very much to you and many thanks to salem
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC