Jul 29, 2013 at 4:49pm Jul 29, 2013 at 4:49pm UTC
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?
Last edited on Jul 30, 2013 at 1:09am Jul 30, 2013 at 1:09am UTC
Jul 29, 2013 at 5:49pm Jul 29, 2013 at 5:49pm UTC
Yes, but your professor will fail you for using it instead of doing it yourself.
Jul 30, 2013 at 1:03am Jul 30, 2013 at 1:03am UTC
This is a personal project. I don't have enough money to go back to college for this.
Jul 30, 2013 at 2:41am Jul 30, 2013 at 2:41am UTC
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:44am UTC
Jul 30, 2013 at 2:42am Jul 30, 2013 at 2:42am UTC
You're exactly on the right track.
Jul 30, 2013 at 2:59am Jul 30, 2013 at 2:59am UTC
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 Jul 30, 2013 at 3:43am UTC
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 Jul 30, 2013 at 3:53am UTC
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 Jul 30, 2013 at 4:18am UTC
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 3:00pm Jul 30, 2013 at 3:00pm UTC
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 Jul 30, 2013 at 3:40pm UTC
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 3:40pm UTC
Jul 30, 2013 at 4:40pm Jul 30, 2013 at 4:40pm UTC
@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 4:42pm UTC
Jul 30, 2013 at 5:21pm Jul 30, 2013 at 5:21pm UTC
Okay this makes slightly more sense to me now, thanks again for the help everyone.