Solved

Return Keyboard Hex Value

Posted on 2004-09-01
3
803 Views
Last Modified: 2009-07-29
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
Comment
Question by:fattumsdad
  • 3
3 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 11952899
How about

#include <iomanip>

and change the cout to

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

-bcl
0
 
LVL 11

Expert Comment

by:bcladd
ID: 11952917
Or, if you want the 0x:

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

0
 
LVL 11

Accepted Solution

by:
bcladd earned 300 total points
ID: 11953046
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now