Simple input to hex display

What is the absolute simplest way to be able to input text into a console and have it convert and display the hexadecimal?

Would you use?
 
std::hex
Last edited on
Yes, but your professor will fail you for using it instead of doing it yourself.
This is a personal project. I don't have enough money to go back to college for this.
You're right. Displaying something in its hex representation is most easily done using std::hex.
Note that std::hex is equivalent to std::setbase(16).

Ex:
1
2
int x = 55;
std::cout << std::hex << x << std::endl;
37


Although if it's a school assignment I would agree with Duoas :p
Like I said, personal project. I wouldn't make it long in a programming class.

So what I want to do is be able to type in text, in this example, "Hello", then have the console print out the hexadecimal form, "68656c6c6f". But I want to be able to type it in and have it convert my input, not have the program pre-set to automatically tell me what "hello" is.

Am I on the right track here?
1
2
3
4
5
6
7
int main ()
{
    int x;
        cin >> x;
std::cout << std::hex << x;
    system("pause");
}


(Yes I know using system() is a huge no-no, but for this application it's acceptable to me.)
Last edited on
You're exactly on the right track.
Okay, all that bit of code seems to be doing is producing "75549e34" which translates to "uTž4", no matter what is input.
What did I do wrong?
You are not on the right track if it is text you want to type in instead of numbers.
In that case I am trying to convert ASCII into hex, I'm guessing that's significantly more complicated?
So you want to read in a text string and output the hex value of each character's ASCII code? You could read into a string using getline() then print each character in hex mode like you were doing above.
http://ideone.com/GbX4qq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <iostream>

void output_hex(char ch)
{
   std::cout << std::hex << int(ch);
}

int main(int argc, char **argv)
{
   std::string str("hello");
   std::for_each(str.begin(), str.end(), output_hex);
   return 0;
}
Thanks for the replies and help guys.

Cire, your example is essentially what I want, but how come when I try to run that code it tells me: "Expected primary expression before "auto" "?
auto is a new c++ 11 feature. Your compiler may require some flags on the command line to make use of it.
Last edited on
@TNX744:
There are actually two C++11 features in play. auto and the range-based for loop.

The following is equivalent:

1
2
3
4
5
6
7
8
9
10
11
12
13
void printCharsAsHex(std::ostream& os, const std::string& s)
{
    for (unsigned i = 0; i < s.length(); ++i)
    {
        char ch = s[i];

        if (ch != ' ' && !std::ispunct(ch))
            os << std::hex << (int) ch;
        else
            os << ch;
    }
    os << '\n';
}


@kevinkjt2000 - I wouldn't have posted the link above if your post previous to it had been visible to me at the time.
Last edited on
Okay this makes slightly more sense to me now, thanks again for the help everyone.
Topic archived. No new replies allowed.