Re: Changing to caps - Dec(Key, 32) ?
- From: "Tom de Neef" <tdeneef@xxxxxxxx>
- Date: Sun, 28 May 2006 21:34:14 +0200
"JamesR" <wizzer90@xxxxxxxxxxx> schreef in bericht
news:1148834911.605648.206780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi
I've come across some code to change the characters typed into an Edit
box to uppercase:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key >= 'a' then
Dec(key, 32);
end;
This works fine, but i'm wonder HOW is actually works. I don't
understand it at all - the Dec procedure seems to decrement an 'ordinal
type' (number right?), and subtract the second parameter from this. So
how come here, we are passing a Char? why does that work?
Check Help - ordinal type: Ordinal types include integer, character,
Boolean, enumerated, and subrange types.
I.e. types whose values are stored as a byte/integer value.
Check Help - Dec: The Dec procedure subtracts one or N from a variable. X is
a variable of an ordinal type, or a pointer type if the extended syntax is
enabled.
So DEC will work on a Char. The Char is stored as a byte value. DEC will
decrement that value. When it is to be presented, the character
representation of that new value is shown.
Also - why does taking the number 32 away from the char automatically
mean it is now uppercase?
The byte value of 'a' is 97 (check with Ctrl+F7 somewhere during execution -
type ord('a')). The byte value of 'A' is 65, i.e. 32 lower. That is true for
all letters in the alphabeth.
Note that this code should not be used in a more general setting, since it
will give wrong results when you happen to be using multi-byte characters
(remarkably, it still works with diacritics: é, ç become É and Ç). The
standard functions are Uppercase and ANSIuppercase.
Finally - if Key is greater than or equal to 'a' ...? what does this
mean, is number 1 greater than or equal to a? or.. hmm.
Again, the operator has a wider scope than just integers. You can compare
'b'<'h', false<true, etc. Read it as ord('b')<ord('h') and
ord(false)<ord(true). ORD returns the byte/integer representation.
Tom
.
- References:
- Changing to caps - Dec(Key, 32) ?
- From: JamesR
- Changing to caps - Dec(Key, 32) ?
- Prev by Date: Re: New event handler for existing component
- Next by Date: Re: Changing to caps - Dec(Key, 32) ?
- Previous by thread: Re: Changing to caps - Dec(Key, 32) ?
- Next by thread: Re: Changing to caps - Dec(Key, 32) ?
- Index(es):
Relevant Pages
|
|