944,106 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2225
  • C++ RSS
Aug 13th, 2007
0

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

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AdventChildren0 is offline Offline
3 posts
since Aug 2007
Aug 13th, 2007
0

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

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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 13th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AdventChildren0 is offline Offline
3 posts
since Aug 2007
Aug 13th, 2007
0

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

What errors did you get?
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 13th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AdventChildren0 is offline Offline
3 posts
since Aug 2007
Aug 13th, 2007
0

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 14th, 2007
0

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

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)
C++ Syntax (Toggle Plain Text)
  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

Quote ...
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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

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: problems with reading in file
Next Thread in C++ Forum Timeline: nodes





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


Follow us on Twitter


© 2011 DaniWeb® LLC