Convert Char into AnsiChar or WideChar (Delphi)

孤街浪徒 提交于 2019-12-12 18:09:45

问题


I'm upgrading a very old (10+ years) application to the latest Delphi XE. There are a number of errors I keep getting like

Incompatible types: 'WideChar' and 'AnsiChar'

I have been just casting the char to the right type: ex. AWideChar = WideChar(fncReturnsChar);

Is this going to cause problems?


回答1:


There might be problems in there for you. Here is a white paper on Unicode in Delphi by Marco Cantù.

http://edn.embarcadero.com/article/38980




回答2:


var
    Initials: String[10];
    FullName: String;

begin

    Initials[1] := FullName[1]; // Error here after Delphi 2009

end;

The problem is that String[10] is the type of AnsiString in later Delphi versions. You are going to assign a Unicode character to an ANSI character in the above code.

The solution is a simple type cast:

Initials[1] := AnsiChar(FullName[1]);

Please refer the document recommended in Mikael Eriksson's answer. That is essential.




回答3:


var
  C : Char;
  AC : AnsiChar;
begin
  AC := '1';
  // C := AC; Delphi does not know how to convert ANSI to Unicode without a codepage
  C := String(AC)[1]; // Any way we can do that by default ANSI decoder
end.


来源:https://stackoverflow.com/questions/4997142/convert-char-into-ansichar-or-widechar-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!