Program to go from a character string to Binary, Octal, and Hexidecimal

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

Join Date: Aug 2007
Posts: 3
Reputation: AdventChildren0 is an unknown quantity at this point 
Solved Threads: 0
AdventChildren0 AdventChildren0 is offline Offline
Newbie Poster

Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #1
Aug 13th, 2007
Ok so far I got this, it takes a string of characters and prints out their binary representations. I am semi new to C++ and I cant for the life of me figure out how to get the hexidecimal and octal representations of the input.

Here is the code so far:

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. void displayBinary(unsigned);
  8.  
  9.  
  10. void main()
  11. {
  12. string input;
  13. cout << "Enter a string: In other words start typing. Use @# for the escape sequence." << endl;
  14.  
  15. while(input != "@#")
  16. {
  17. getline(cin, input);
  18.  
  19. for(int i =0; i < input.length(); i++)
  20. {
  21. cout << input.at(i) << " = ";
  22. displayBinary((unsigned)input.at(i));
  23. }
  24.  
  25. cout << endl;
  26. }
  27. }
  28.  
  29. void displayBinary(unsigned u)
  30. {
  31. register int b;
  32.  
  33. for(b = 128; b > 0; b = b/2)
  34. {
  35. (u & b)?(cout << '1'):(cout << '0');
  36. }
  37. cout << " ";
  38. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #2
Aug 13th, 2007
Instead of dividing by 2, divide by 8 or 16. That gives you octal and hexadecimal digits, respectively. The most obvious way to get the string is to build it in reverse by chopping off the least significant digit and using it to index an array of suitable translated digits.
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string changeBase( unsigned int ch, unsigned int base ) {
  7. const string digits = "0123456789ABCDEF";
  8.  
  9. string result = "";
  10.  
  11. while ( ch != 0 ) {
  12. result = digits[ch % base] + result;
  13. ch /= base;
  14. }
  15.  
  16. return result;
  17. }
  18.  
  19. int main() {
  20. string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  21.  
  22. for ( string::size_type i = 0; i < s.size(); ++i ) {
  23. cout<< changeBase( s[i], 8 ) <<"\n";
  24. }
  25.  
  26. return 0;
  27. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3
Reputation: AdventChildren0 is an unknown quantity at this point 
Solved Threads: 0
AdventChildren0 AdventChildren0 is offline Offline
Newbie Poster

Re: Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #3
Aug 13th, 2007
I tried to use that code to see what the outcome with but but it blew up with alot of errors.
Last edited by AdventChildren0; Aug 13th, 2007 at 8:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #4
Aug 13th, 2007
What errors did you get?
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3
Reputation: AdventChildren0 is an unknown quantity at this point 
Solved Threads: 0
AdventChildren0 AdventChildren0 is offline Offline
Newbie Poster

Re: Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #5
Aug 13th, 2007
nvm the errors cleared but the problem is it gives out 32 outputs. (your code) I assume 1-26 = A-Z but what is the other 6? Also your code isnt very practical to use with the code I presented. I would have to re-engineer your code to put it into my program.
Last edited by AdventChildren0; Aug 13th, 2007 at 9:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #6
Aug 13th, 2007
  1. #include <iostream>
  2. #include <string>
  3. using namespace std ;
  4.  
  5. int main()
  6. {
  7. string str ;
  8. getline( cin, str ) ;
  9. cout << str << '\n' ;
  10.  
  11. cout << oct << showbase ;
  12. for( size_t i=0 ; i<str.size() ; ++i ) cout << int( str[i] ) << ' ' ;
  13. cout << '\n' ;
  14.  
  15. cout << hex << showbase ;
  16. for( size_t i=0 ; i<str.size() ; ++i ) cout << int( str[i] ) << ' ' ;
  17. cout << '\n' ;
  18. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: Program to go from a character string to Binary, Octal, and Hexidecimal

 
0
  #7
Aug 14th, 2007
Originally Posted by AdventChildren0 View Post
nvm the errors cleared but the problem is it gives out 32 outputs. (your code) I assume 1-26 = A-Z but what is the other 6?
The output should have been the characters 'A' to 'Z' in octal, 101-132 (that's 26 numbers, not 32). What output did you get? did you run the example exactly as it was shown?

This is what I got when I ran Hamrick's program (Copy & pasted from console output window)
  1. 101
  2. 102
  3. 103
  4. 104
  5. 105
  6. 106
  7. 107
  8. 110
  9. 111
  10. 112
  11. 113
  12. 114
  13. 115
  14. 116
  15. 117
  16. 120
  17. 121
  18. 122
  19. 123
  20. 124
  21. 125
  22. 126
  23. 127
  24. 130
  25. 131
  26. 132

Also your code isnt very practical to use with the code I presented. I would have to re-engineer your code to put it into my program.
Don't expect people to provide complete copy & paste examples that you can just use without thinking. If you can understand what's going on in the example, it should be a piece of cake for you to change the code to suit your program, otherwise, point out the bits that you're struggling with and someone might be able to guide you further.
¿umop apisdn upside down?
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: 1712 | Replies: 6
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC