开发者

convert string character to ascii in delphi

开发者 https://www.devze.com 2022-12-20 16:21 出处:网络
How can i convert string character (12开发者_Go百科3-jhk25) to ASCII in Delphi7If you mean the ASCII code for the character you need to use the Ord() function which returns the Ordinal value of any \"

How can i convert string character (12开发者_Go百科3-jhk25) to ASCII in Delphi7


If you mean the ASCII code for the character you need to use the Ord() function which returns the Ordinal value of any "enumerable" type

In this case it works on character values, returning a byte:

var
  Asc : Byte;
  i : Integer;
begin
  for i := 1 to Length(s) do
  begin
    Asc := Ord(s[i]);
    // do something with Asc
  end;
end;


It depends on your Delphi version. In Delphi 2007 and before, strings are automatically in ANSI string format, and anything below 128 is an ASCII character.

In D2009 and later, things become more complicated since the default string type is UnicodeString. You'll have to cast the character to AnsiChar. It'll perform a codepage conversion, and then whatever you end up with may or may not work depending on which language the character in question came from. But if it was originally an ASCII character, it should convert without trouble.

0

精彩评论

暂无评论...
验证码 换一张
取 消