Re: Numeric data in a CString object
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Tue, 07 Mar 2006 19:48:57 -0500
You have probably learned in other languages where characters and integers are different
types. As far as C is concerned, the following are all the same:
0x41
65
0101
'A'
all represent a numerical value of decimal 65. So there is no "magic" to getting the
ASCII code of a character. Any integer value can be compared to any character literal,
and fetching a character from a CString yields an integer, e.g.,
CString cat = "Cat";
ASSERT(cat[1] == 'a');
ASSERT(cat[1] == 97);
ASSERT(cat[1] == 0141);
ASSERT(cat[1] == 0x61);
would result in valid compilations with no errors. at run time.
There are deeper questions here. For example, how did the data get into the CString? If
you are coming from an edit control, you could simply set the "Numeric" property to TRUE
and then the user can only type in the values 0 thru 9. This doesn't work, however, if
you need signed values, or decimal points. The solution of using SpanIncluding works if
you don't mind syntactic errors, e.g., it will happily say the string
0.1.2.3+4
is valid. You end up with the same problem if you subclass CEdit and only allow the
characters 0 1 2 3 4 5 6 7 8 9 + - . to be typed into the control. That's why I created
my validating edit control; it also handles properly doing a paste. But the heart of it
is just scanning a CString and seeing if it is a valid number.
Generally, if you are trying to do input validation, you need to use something like a
Finite State Machine (FSM) to parse the string to see if it is really valid. There is a
floating-point string parser in my Validating Edit Control on my MVP Tips site; note that
it is parsing a CString variable for syntactically valid floating-point numbers. Depending
on the options, it will allow a sign, some number of decimal digits, and an exponent.
It's a good idea to learn about finite state machines; they are a very useful "tool" to
understand how to use. They are the core of most lexical processing, such as reading the
input of a programming language and splitting out the identifiers, constants, operators,
reserved words, etc. They are the simplest of the "recognition automata". The other
useful one is the Deterministic Push-Down Automaton, or DPDA, the basic parser of nearly
all programming languages, such as C, Pascal, Basic, etc., but that's something you can
spend a lot of time studying. There's a massive amount of information out on the
Internet, primarily class notes for programming language courses, that would help. Also
for FSMs, which have a huge body of theory. But the coding can be quite simple; study my
example to see how you might go about validation.
joe
On Mon, 6 Mar 2006 17:55:27 +0100, "Mittik" <accia77@xxxxxxxxxxx> wrote:
Joseph M. Newcomer [MVP]
Ajay Kalra wrote:
You
will need to go thru the string yourself and check if each digit is a
number.
How can i do that?
I was thinking about using the ASCII code,but how can i retrieve the ascii
code of a character?
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Numeric data in a CString object
- From: Mittik
- Re: Numeric data in a CString object
- From: Ajay Kalra
- Re: Numeric data in a CString object
- From: Mittik
- Numeric data in a CString object
- Prev by Date: Re: Post Message from Property *** to another
- Next by Date: Re: Using DLLs
- Previous by thread: Re: Numeric data in a CString object
- Next by thread: Re: How to get ID of a button & bitmap in a BitmapButton???
- Index(es):