Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return Keyboard Hex Value

Posted on 2004-09-01
3
Medium Priority
?
832 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 1200 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

610 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