String to binary/dec/hex/oct

closed account (4Gb4jE8b)
Doesn't matter which one really. I want to try my hand at encoding something, so i chose a word, and tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    string ss;
    cout << "Enter something\n";
    getline(cin,ss);
    cout << ss << endl;
    cout << hex << ss << endl;
    cout << dec << ss << endl;
    cout << oct << ss << endl;
    cin.get();
    cin.get();
    return 0;
}


however the output is the exact same as the input. I've done some rudimentary searches, but have yet to find anything.

note* currently compiling on dev cpp, because i didn't feel like making a project and everything for msvc2010. I can switch if this idea won't work on dev.

Thanks for your help guys
The hex, dec, and oct manipulators only apply to integers, not to strings.
You would need to cast the string into an int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    string ss;
    cout << "Enter something\n";
    getline(cin,ss);
    cout << ss[0] << endl;
    cout << hex << int(ss[0]) << endl;
    cout << dec << int(ss[0]) << endl;
    cout << oct << int(ss[0]) << endl;
    cin.get();
    cin.get();
    return 0;
}


This will display the first character in the string in the way you wish to display it. I trust you will be able to modify appropriately to display every char in a string.
closed account (4Gb4jE8b)
You trust correctly, thank you :)
Good grief! Do it right! (You can't "cast a string into an int".)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iomanip>
#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );

    cout << "hex = " << hex << n << endl;
    cout << "dec = " << dec << n << endl;
    cout << "oct = " << oct << n << endl;

    cout << "Press ENTER to quit.";
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );

    return 0;
}

Good luck.
closed account (4Gb4jE8b)
Well upon further reading, I found an interesting way of doing what i wanted to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    int ascii;
    int binary[8];
    char letter;

    for(int i = 0; i< ss.length(); i++)
    {
		letter = ss[i];
		ascii = letter;
		while(ascii > 0)
		{
             for(int j = 0; j < 8; j++)
             {
                if((ascii%2) == 0)
                {
                      ascii = ascii/2;
                      binary[j] = 0;
                }
                else
                {
                      ascii = ascii/2;
                      binary[j] = 1;
                }


for the binary example at least. Hex/dec/oct were much easier as after feeding the letter into an int to get the code, you just set a stream with iomanip to the particular setting you want.

note* i know that binary holds the actual binary code backwards, i figured out how to take care of that as well.
Last edited on
Topic archived. No new replies allowed.