943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16972
  • C++ RSS
Jan 23rd, 2005
0

Base converter

Expand Post »
I have written this program to convert a decimal number to a number of any base. The resulting number has been put into an array and so can be of any size. But the decimal number that i take as input is limited by the range of int. What i want to do is to take it as a string and then change its base. The challenge is that i then cant divide and check for quotients and remainders as i can do with normal integers. What should i do?

This is what i got so far:
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. int convert_to_base(int actual_number, int base, int **number);
  7.  
  8. int main()
  9. {
  10.  
  11. int anumber = 0;//the actual number
  12. int base;
  13. bool negative = false;
  14.  
  15. cout<<"\nStarted Base Converter --- Kazi Asif Wadud --- Jan 20, 2005\n";
  16.  
  17. cout<<"\nUse Ctrl+C to exit";
  18.  
  19. while(true)
  20. {
  21.  
  22. cout<<"\n\nEnter Number to convert: ";
  23. cin>>anumber;
  24.  
  25. while(true)
  26. {
  27. cout<<"\t\tTo base: ";
  28. cin>>base;
  29. if(base >= 2)//Error checking for the base
  30. break;
  31. else
  32. cout<<"Base must be greater than or equal 2\n";
  33. }
  34.  
  35. cout<<"\t\t Result: ";
  36.  
  37. int *number;
  38. int size;
  39.  
  40. if(anumber < 0)
  41. {
  42. negative = true;
  43. anumber = -anumber;
  44. }
  45.  
  46. size = convert_to_base(anumber, base, &number);
  47.  
  48. if(negative)
  49. cout<<"-";
  50. for(int i = 0; i < size; ++i)
  51. cout<<number[i];
  52.  
  53.  
  54. }
  55.  
  56. return 0;
  57. }
  58.  
  59. int convert_to_base(int actual_number, int base, int **number)
  60. {
  61. //Makes the array 0 if the actual number is zero
  62. if (actual_number == 0)
  63. {
  64. *number = new int;
  65. *number[0] = 0;
  66. return 1;
  67. }
  68.  
  69. //Computing the required size of the array
  70. //to hold the converted number
  71. double size;
  72.  
  73. size = log10(actual_number) / log10(base) + 1;
  74.  
  75. //Allocating memory
  76. *number = new int[size];
  77.  
  78.  
  79. //The conversion takes place here
  80. int i = size -1;
  81. for(int quotient = actual_number; quotient > 0; quotient = quotient / base, --i)
  82. *(*number + i) = quotient % base;
  83.  
  84. return (int)size;
  85. }
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jan 23rd, 2005
0

Re: Base converter

Option 1: Write your own functions to manipulate numbers represented as a string.
Option 2: Use a library that does Option 1, such as GMP.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 23rd, 2005
0

Re: Base converter

I have this vague idea that when developing an arbitrary precision arithmetic it is necessary to convert a number to a higher base. The number if converted to a higher base will require less digits and so we will get an increase in the speed of multplication, addition etc. Actually my reason behind writing this base conversion program was a first attempt towards developing an arbitrary precision arithmetic, but unfortunately this program itself depends on division and i might need to define division to arbitrary precision before attempting anything further; looks like i m going in circle. So i would not go for GMP right away, could u assist me go further without it?
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Jan 23rd, 2005
0

Re: Base converter

>could u assist me go further without it?
Sure. It's simple, but tedious. All you need to do is figure out the manual steps for division, then loop from the back of the string to the front and perform each of the steps. Alternatively, you could reverse the string and work from the front back, or solve the problem through repeated subtractions because subtraction is a trivial modification of addition and addition is easy.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 26th, 2005
0

Re: Base converter

How did GMP do it? Did they convert the input into a higher base? How about other precision systems(mathematica, maple) and how do they do it? I would really like to know in through.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: question about rand() function
Next Thread in C++ Forum Timeline: Tic Tac Toe AI help, where to reset variables.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC