Solved

Return Keyboard Hex Value

Posted on 2004-09-01
3
820 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
is twain_32.dll cmpatible with windows 10 ? 10 239
gdb doesn't stop on breakpoint 2 114
designing in object programming 12 110
boost::uuid crashes 17 41
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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