Arrays and functions help -- base converter

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

Join Date: Mar 2009
Posts: 6
Reputation: fourstar is an unknown quantity at this point 
Solved Threads: 0
fourstar fourstar is offline Offline
Newbie Poster

Arrays and functions help -- base converter

 
0
  #1
Jul 3rd, 2009
Hi there. I'm trying to write a program that will convert any number from base x to base y (bases 2-16). The user will also be able to convert hexadecimal numbers like 4BFA from base 16 to base 2.. which is 100101111111010.

So far I've began to prompt the user for the number, ask for the old base, the new base. I am having trouble trying to convert the numbers that are input into a char array into integers so I can perform calculations on them. Essentially, I'd like to scan through each of the letters if it's a hex string and replace those with the equivalent numerical value.

Here is the code I have so far:

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char numIn[15];
  9. int num[15];
  10. char values[17] = "0123456789ABCDEF"; // array used for table lookup
  11. int ob, nb, i;
  12.  
  13. cout << "Enter the the number you want to convert: ";
  14. cin >> numIn;
  15.  
  16. // convert string to upper case *note* i'm using _strupr_s because vc '08 likes to give me a nasty deprication warning
  17. _strupr_s(numIn);
  18.  
  19. cout << "\n Enter the original base: ";
  20. cin >> ob;
  21.  
  22. // range checking
  23. if(ob < 2 || ob > 16)
  24. {
  25. cout << "Bases are 2-16. Please re-enter:" << endl;
  26. cin >> ob;
  27. }
  28.  
  29. cout << "\n Enter the new base: ";
  30. cin >> nb;
  31.  
  32. // range checking
  33. if(nb < 2 || nb > 16)
  34. {
  35. cout << "Bases are 2-16. Please re-enter:" << endl;
  36. cin >> nb;
  37. }
  38.  
  39. // convert the char array to string
  40. string s;
  41. s = numIn;
  42. cout << "\nAfter converting the char array, the string is: " << s << endl;
  43.  
  44. // find the string length for the loop
  45. int length = strlen(numIn);
  46. cout << "\nThe length of the string is: " << length << endl;
  47.  
  48. // loop through each subscript of the input
  49. for(i=0; i < length; i++)
  50. {
  51. cout << num[i] << " ";
  52.  
  53. // check to see if it's a letter or number
  54. if(isdigit(numIn[i]) == true)
  55. {
  56. num[i] = numIn[i] - 48;
  57. }
  58. else
  59. {
  60. num[i] = numIn[i] - 55;
  61. }
  62. }
  63.  
  64. // reverse the string for later use
  65. cout << "\nAfter reversing the string, the string is: ";
  66. for(i = length - 1; i >= 0; i--)
  67. {
  68. cout << numIn[i];
  69. }
  70. cout << endl;
  71.  
  72. return 0;
  73. }

I know I still need my calculations, but I'd like to do this the right way before I start into that other part.

Any help would be greatly appreciated!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Arrays and functions help -- base converter

 
0
  #2
Jul 3rd, 2009
fourstar,

I read your program. As you stated that some calculations are needed - my suggestion for you to write such a function that convert string (numeric values) into int and vice versa.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Arrays and functions help -- base converter

 
0
  #3
Jul 3rd, 2009
You can use this in your function :

* Test if string[i] is a character and then decimal value of that character (assumed to be a hex value A-F or a-f) can be done by :
dec_value = 10+(int)((int)string[i]-65) (for upper case) or
dec_value = 10+(int)((int)string[i]-97) (for lower case) .

With the above procedure you'll get the decimal value of your hex digit now you can go on with your computations without any problem right ???
Last edited by csurfer; Jul 3rd, 2009 at 8:10 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: fourstar is an unknown quantity at this point 
Solved Threads: 0
fourstar fourstar is offline Offline
Newbie Poster

Re: Arrays and functions help -- base converter

 
0
  #4
Jul 3rd, 2009
Ok thanks for the help. I got that part figured out now. It turns out I was using

  1. if(isdigit(numIn[i]) == true)

and I shouldn't have been using
  1. == true

Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,840
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Arrays and functions help -- base converter

 
0
  #5
Jul 3rd, 2009
Originally Posted by fourstar View Post
Ok thanks for the help. I got that part figured out now. It turns out I was using

  1. if(isdigit(numIn[i]) == true)

and I shouldn't have been using
  1. == true

Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?

Ascii value of 'G' is 71. Ascii value of 'Z' is 90. Any character between 71 and 90, inclusive, is to be rejected.

  1. char digit = 'J';
  2. if (digit >= 71 && digit <= 90)
  3. cout << "Illegal digit";

Have you considered using this function?
http://www.cplusplus.com/reference/c...type/isxdigit/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 475 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC