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:

``````#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.

``````#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;
}``````

I tried to use that code to see what the outcome with but but it blew up with alot of errors.

What errors did you get?

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.

``````#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?

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)

``````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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.