Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 838
  • Last Modified:

Return Keyboard Hex Value

The following code (provided by bcladd on my last question) will return the value of any key pressed.  How do I get the program to return the hexadecimal value when a key is pressed instead (0x00 etc)?  As always, I appreciate any and all help.  Too bad they didn't award point for ASKING questions...  I'd be in the lead :-)

#include <conio.h>
using namespace std;

int main()
{
  char ch;
  while (ch = getch()) {

    cout << (int)ch << endl;
    if (ch == 'X') break;
  }
  return 0;
}

Regards,
Tony
0
fattumsdad
Asked:
fattumsdad
  • 3
1 Solution
 
bcladdCommented:
How about

#include <iomanip>

and change the cout to

cout << hex << (int)ch << endl;

-bcl
0
 
bcladdCommented:
Or, if you want the 0x:

cout << "0x" << hex << (int)ch << endl;

0
 
bcladdCommented:
Last followup to my own stuff, I promise:

    cout << "0x" << setfill('0') << setw(2) << hex << (int)ch << endl;

Prints the leading zero for numbers less than 0x10

-bcl
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now