Re: Delphi ORD Function


[ Post Followup ] [ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]

Posted by webmaster Guido on June 11, 2003

In Reply to: Delphi ORD Function posted by Brett Kingston on June 09, 2003

: What data type is required for Delphi's ORD Function?
: I have this working if I use a static value in the code (i.e. s := IntToStr(Ord('#'));), but if I want # to be replaced by the value of an input box, I keep getting errors. I have tried converting the input to a string, real, integer, but I have had no luck.
: I have tried this:
: var
: i : integer;
: s : string;
: begin
: i := inputChar.Text
: s := IntToStr(Ord(i));
: lblNum.Caption := S;
: end;
: end.
: ...but with no luck. More compile errors.
: Can someone help me with an example piece of code?
: Thanks
----------

If InputChar is a TEdit component, then there are two errors:

*    i := InputChar.Text; gives an error because InputChar.Text is of type "string", and you declared i as an "integer".

*    Ord(i) gives an error because ORD() only works on values of type "char" (character), and you declared i as an "integer".

The following will return the ASCII-code of the first character of the Edit-box:

var
  i : integer;
  S: string;
begin
  S := inputChar.Text;
  if Length(S) > 0 then begin
    i := Ord(S[1]); // ASCII code of first character of S
    lblNum.Caption := IntToStr(i);
  end
  else
    lblNum.Caption := 'Please, type something in the Edit-box';
end;



[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]