Solved

How to convert letter to Ascii

Posted on 2013-06-18
3
677 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

760 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

16 Experts available now in Live!

Get 1:1 Help Now