Base converter

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Base converter

 
0
  #1
Jan 23rd, 2005
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Base converter

 
0
  #2
Jan 23rd, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Base converter

 
0
  #3
Jan 23rd, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Base converter

 
0
  #4
Jan 23rd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Base converter

 
0
  #5
Jan 26th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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