Simple input to hex display

Jul 29, 2013 at 4:49pm
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 Jul 30, 2013 at 1:09am
Jul 29, 2013 at 5:49pm
Yes, but your professor will fail you for using it instead of doing it yourself.
Jul 30, 2013 at 1:03am
This is a personal project. I don't have enough money to go back to college for this.
Jul 30, 2013 at 1:18am
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
Jul 30, 2013 at 2:41am
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 Jul 30, 2013 at 2:44am
Jul 30, 2013 at 2:42am
You're exactly on the right track.
Jul 30, 2013 at 2:59am
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?
Jul 30, 2013 at 3:43am
You are not on the right track if it is text you want to type in instead of numbers.
Jul 30, 2013 at 3:53am
In that case I am trying to convert ASCII into hex, I'm guessing that's significantly more complicated?
Jul 30, 2013 at 4:18am
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.
Jul 30, 2013 at 4:36am
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;
}
Jul 30, 2013 at 4:45am
Jul 30, 2013 at 3:00pm
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" "?
Jul 30, 2013 at 3:40pm
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 Jul 30, 2013 at 3:40pm
Jul 30, 2013 at 4:40pm
@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 Jul 30, 2013 at 4:42pm
Jul 30, 2013 at 5:21pm
Okay this makes slightly more sense to me now, thanks again for the help everyone.
Topic archived. No new replies allowed.