• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

How to convert letter to Ascii

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
edvinson
Asked:
edvinson
  • 3
2 Solutions
 
jkrCommented:
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
 
jkrCommented:
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
 
jkrCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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