Casting Delphi 2009/2010 string literals to PAnsiChar

前端 未结 4 1477
一向
一向 2021-02-11 01:33

So the question is whether or not string literals (or const strings) in Delphi 2009/2010 can be directly cast as PAnsiChar\'s or do they need an additional cast to AnsiString fi

4条回答
  •  旧巷少年郎
    2021-02-11 01:46

    As Mason Wheeler points out all is fine as long as you don't have non-ANSI characters in your string const. If you have things like:

    const FRED = 'Frédérick';
    

    I'm pretty sure Delphi 2009/2010 will either issue charset hints (and apply a string conversion automatically - thus the hint) or fail at comparing ('Frédérick' is different in ISO-8859-1 than UTF-16).

    If you can have "special" characters in your consts you will need to call string conversion.

    Here are some basic examples with TStringList:

    TStringList.SaveToFile(DestFilename, TEncoding.GetEncoding(28591)); //ISO-8859-1 (Latin1)
    TStringList.SaveToFile(DestFilename, TEncoding.UTF8);
    

提交回复
热议问题