Board index » delphi » Characters and ASCII codes

Characters and ASCII codes

You can only get the Ord of an ordinal value...is strChar defined as a Char
or a string.  If it's a string then it won't work.

This works.

procedure ....
var
  x: Char;
begin
  x := 'A';
  showmessage(inttostr(Ord(x)));
end;

Doesn't work if x is a string type.  Should give incompatible types error.

Quote
>Hi all,
> Little question:

>Why does this work
> intChar := Ord('A');

>and this doesn't?
> strChar := 'A';
> intChar := Ord(strChar);

>How do you get the ascii number for a single character as this doesn't
>work?

>Thanks

>    ____
>  /    /___ ___ ___ ___
> / ___/   /   / __/ __/
>/ /  / / / / /__ / _/
>____/___/___/___/___/
>Go...@goose-soft.demon.co.uk

 

Re:Characters and ASCII codes


Hi all,
        Little question:

Why does this work
        intChar := Ord('A');

and this doesn't?
        strChar := 'A';
        intChar := Ord(strChar);

How do you get the ascii number for a single character as this doesn't
work?

Thanks

    ____
  /    /___ ___ ___ ___
 / ___/   /   / __/ __/
/ /  / / / / /__ / _/
____/___/___/___/___/
Go...@goose-soft.demon.co.uk

Re:Characters and ASCII codes


On Fri, 06 Aug 1999 16:50:58 GMT, an...@goose-soft.demon.co.uk (Angus

Quote
Herron) wrote:
>    Little question:

>Why does this work
>    intChar := Ord('A');

>and this doesn't?
>    strChar := 'A';
>    intChar := Ord(strChar);

>How do you get the ascii number for a single character as this doesn't
>work?

The Ord function requires as it parameter a value of type Char. A String
value is not the same. But a String value can be treated as an array of
Char values and one element of that array can be passed as the parameter
for the Ord function.

  var
    strChar: String;
    intChar: Byte;
  begin
    strChar := 'A';
    intChar := Ord(strChar[1]);
  end;

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski         "Television is a medium because anything well done
Felton, CA             is rare."
                                                 -- Fred Allen (1894-1956)

Re:Characters and ASCII codes


Quote
Angus Herron wrote:

> Hi all,
>         Little question:

> Why does this work
>         intChar := Ord('A');

> and this doesn't?
>         strChar := 'A';
>         intChar := Ord(strChar);

> How do you get the ascii number for a single character as this doesn't
> work?

> Thanks

>     ____
>   /    /___ ___ ___ ___
>  / ___/   /   / __/ __/
> / /  / / / / /__ / _/
> ____/___/___/___/___/
> Go...@goose-soft.demon.co.uk

To get a single character of a string you must access the character
directly.

intChar:=Byte(strChar[1]);  // puts the ascii code of the first
character into intChar

Alex

--
Author of the free Chatsystem PINO!
Available at http://pino.cjb.net

Re:Characters and ASCII codes


On Fri, 06 Aug 1999 18:09:39 GMT, koter...@NOSPAMgte.net (Steve

Quote
Koterski) wrote:

>The Ord function requires as it parameter a value of type Char. A String
>value is not the same. But a String value can be treated as an array of
>Char values and one element of that array can be passed as the parameter
>for the Ord function.

>  var
>    strChar: String;
>    intChar: Byte;
>  begin
>    strChar := 'A';
>    intChar := Ord(strChar[1]);
>  end;

As it happens what I want to do is to take the Ord of every letter in
a string so the fact that a string can be referenced as an array of
char should solve it.

Thanks,

    ____
  /    /___ ___ ___ ___
 / ___/   /   / __/ __/
/ /  / / / / /__ / _/
____/___/___/___/___/
Go...@goose-soft.demon.co.uk

Re:Characters and ASCII codes


Declare "strChar" as "Char" type, not "String"!!!

Matija

Quote
Angus Herron <an...@goose-soft.demon.co.uk> wrote in message

news:37ab10df.28709627@news.demon.co.uk...
Quote
> Hi all,
> Little question:

> Why does this work
> intChar := Ord('A');

> and this doesn't?
> strChar := 'A';
> intChar := Ord(strChar);

> How do you get the ascii number for a single character as this doesn't
> work?

> Thanks

>     ____
>   /    /___ ___ ___ ___
>  / ___/   /   / __/ __/
> / /  / / / / /__ / _/
> ____/___/___/___/___/
> Go...@goose-soft.demon.co.uk

Other Threads