Delphi: ASCII Codes

Marconi September 11, 2002

How Can I get the ASC code from any CHAR.
Using CHR(65) the result is 'A'.
In Clipper the function ASC do that ASC('A') result 65. But, in Delphi (or Pascal) I don't know how.

Thanks any help.

Re: webmaster Guido

The Delphi function is: ORD.

For example, Ord('A') returns 65

Re: Re: Brett Kingston

What data type is required for this?
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. Can someone help me with an example piece of code?

Re: Re: Re: webmaster Guido

Ord(C) needs a parameter C of type "char" (character). So, this will work:

  AsciiVal: integer;
  C: Char;
  S: string;
  C := 'B';
  AsciiVal := Ord(C); // gives ASCII-code of 'B'
  AsciiVal := Ord(Edit1.Text[1]); // ASCII first character of text in Edit1 
  S := 'Test';
  AsciiVal := Ord(S[2]); // ASCII-code of 'e'

But the following will not work:

  AsciiVal := Ord(Edit1.Text); // error because Edit1.Text is type "string"


[ Related Articles and Replies ] [ DelphiLand Forum ]