Recent

Author Topic: how to return ascii code for char?  (Read 21524 times)

jw

  • Full Member
  • ***
  • Posts: 126
how to return ascii code for char?
« on: April 18, 2010, 11:46:17 pm »
I would like to know how to return ascii code for characters in a string.  The ord function should work but I'm getting
error: incompatible types: got "ansistring" expected char


code


stringy:='abcdefg';

for a:=1 to length(stringy) do

begin

     edit2.text:=edit2.text + inttostr(ord(Copy(stringy,a,1)));

end;

I cant find a string manipulation command that returns a char to use in place of copy.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9210
  • Debugger - SynEdit - and more
    • wiki
Re: how to return ascii code for char?
« Reply #1 on: April 19, 2010, 12:06:09 am »
ord(stringy[a])

However this returns the byte (not char) at the given position. But copy does the same, copy accesses the string by bytes.

so as soon as any accented, umlaut, or other such char occurs in you string, that will break. Because by default Lazarus uses UTF8, and chars can use between 1 and 4 bytes.

Then again for UTF8 chars there is no ascii-code (and no function like ord).

in LCLProc there are a few string handling functions like UTF8Length.

jw

  • Full Member
  • ***
  • Posts: 126
Re: how to return ascii code for char?
« Reply #2 on: April 20, 2010, 08:19:15 pm »
the stringy[a] was what I couldn't fugure out access the stringy like an array thanks!

 

TinyPortal © 2005-2018