Solved

How to convert letter to Ascii

Posted on 2013-06-18
3
696 Views
Last Modified: 2013-06-18
I have an edit control, EDC_EDIT11 which the user will likely input a number.

How would I check to see if they entered the Ascii value of the letter 'c' , for instance?

Basically I need to convert what they entered to a letter.

Thanks!
0
Comment
Question by:edvinson
[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 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 39256722
A letter is it's own ASCII value by definition (see http://en.wikipedia.org/wiki/ASCII), so ist's simply

char c = 'c';

int ascii_val = (int) c;

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39256887
Oh, BTW, C++ has a simple way to check that using 'isdigit()' (http://msdn.microsoft.com/en-us/library/fcc4ksh8%28v=VS.71%29.aspx). However, an easier way is to try to convert the string into a number and see if it's successfull, e.g.

#include <sstream>
using namespace std;

typedef basic_stringstream<TCHAR> tstringstream;
// ...

TCHAR buf[256];

GetDlgItemText(hwndDlg,EDC_EDIT11,buf,256);

tstringstream ss(buf);
int test;

ss >> test;

if (ss.fail()) {

  // not a numeric value, error

} else {

  // 'test' now contains the numeric value that EDC_EDIT11 holds

}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39256900
Fro the sake of completeness: That check and conversion can also be done using 'strtol()' (http://msdn.microsoft.com/en-us/library/w4z2wdyc(v=VS.71).aspx):

		int n = (int)strtol(buf,&pszCnvEnd,10);

		if ( *pszCnvEnd) { // invalid format, not a number

		} else {

                 // 'n' now contains the numeric value that EDC_EDIT11 holds
               }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…

691 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