0

Possible Duplicate:
how do I print an unsigned char as hex in c++ using ostream?
Convert ASCII string into Decimal and Hexadecimal Representations

I want to print out the hex value of characters using isprint(). However, I cannot get it to work. This is my attempt:

getline(cin, w);
for(unsigned int c = 0; c < w.size(); c++)
{
  if(!isprint(w[c]))
  {
    cout << "ERROR: expected <value> found " << hex << w[c] << endl;
    return 0;
  }
}

Can anyone help me print out this hex value? Thanks! I'm inputting things like:

í

and I want it's hex value.

Community
  • 1
  • 1
user1567909
  • 1,450
  • 2
  • 14
  • 24

1 Answers1

3

By default, a char is printed as an string character. Try casting the char to a general int like this:

cout << "ERROR: expected <value> found " << hex << static_cast<int>(w[c]) << endl;
vz0
  • 32,345
  • 7
  • 44
  • 77