Solved

How to convert letter to Ascii

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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.

832 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