Re: Changing to caps - Dec(Key, 32) ?



"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


.



Relevant Pages

  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... Why is using char* a bad thing and why using sprintf a bad thing to, ... can be up to MAX_PATH characters). ... LPSTR lpMsgBuf; ... MessageBox(NULL, lpMsgBuf, "GetLastError() for ...
    (microsoft.public.vc.mfc)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... This means that if you develop the bad habit of using char * (left over ... It usually takes me five minutes to create a Unicode version of any of my apps, ... BOOL and bool are different data types. ... can be up to MAX_PATH characters). ...
    (microsoft.public.vc.mfc)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.server)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.programming)
  • Re: RfD: XCHAR wordset (for UTF-8 and alike)
    ... extended to work with xchars, ... >replacing one char with another. ... before-cursor part, even for fixed-width characters. ... So, should string words ...
    (comp.lang.forth)