| | |
Program to go from a character string to Binary, Octal, and Hexidecimal
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 3
Reputation:
Solved Threads: 0
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:
Here is the code so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <iomanip> using namespace std; void displayBinary(unsigned); void main() { string input; cout << "Enter a string: In other words start typing. Use @# for the escape sequence." << endl; while(input != "@#") { getline(cin, input); for(int i =0; i < input.length(); i++) { cout << input.at(i) << " = "; displayBinary((unsigned)input.at(i)); } cout << endl; } } void displayBinary(unsigned u) { register int b; for(b = 128; b > 0; b = b/2) { (u & b)?(cout << '1'):(cout << '0'); } cout << " "; }
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)
#include <iostream> #include <string> using namespace std; string changeBase( unsigned int ch, unsigned int base ) { const string digits = "0123456789ABCDEF"; string result = ""; while ( ch != 0 ) { result = digits[ch % base] + result; ch /= base; } return result; } int main() { string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for ( string::size_type i = 0; i < s.size(); ++i ) { cout<< changeBase( s[i], 8 ) <<"\n"; } return 0; }
The truth does not change according to our ability to stomach it.
•
•
Join Date: Aug 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std ; int main() { string str ; getline( cin, str ) ; cout << str << '\n' ; cout << oct << showbase ; for( size_t i=0 ; i<str.size() ; ++i ) cout << int( str[i] ) << ' ' ; cout << '\n' ; cout << hex << showbase ; for( size_t i=0 ; i<str.size() ; ++i ) cout << int( str[i] ) << ' ' ; cout << '\n' ; }
•
•
•
•
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?
This is what I got when I ran Hamrick's program (Copy & pasted from console output window)
C++ Syntax (Toggle Plain Text)
101 102 103 104 105 106 107 110 111 112 113 114 115 116 117 120 121 122 123 124 125 126 127 130 131 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.
¿umop apisdn upside down? ![]() |
Similar Threads
- the middle character of a string (C++)
- String -> Binary -> TextBox -> AARGH! (C#)
- conversion of string into binary (C#)
- conversion of string into binary (C#)
- Read a string from a binary file into a C or C++ string (C++)
- Finding the Most Common Character in a String (Java)
- Writing a program that uses a Character Queue (C++)
Other Threads in the C++ Forum
- Previous Thread: problems with reading in file
- Next Thread: nodes
Views: 1712 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






