Solved

How to convert letter to Ascii

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 89
Finding Divisors 5 57
C++ statement T∗ begin(Vector<T>& x) 5 38
MFC COM Server not showing  form 4 21
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 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