A few years ago, the default character type in Delphi was changed from AnsiChar (a single-byte variable representing the ANSI character) to WideChar (a two-byte variable representing the UTF16 character.) The char type is now an alias of WideChar instead of AnsiChar , the string type is now an alias of UnicodeString (UIF- 16 Unicode versions of the traditional Delphi string type) instead of AnsiString , and PChar > is now an alias of PWideChar instead of PAnsiChar .
The compiler can take care of a large number of conversions themselves, but there are several problems:
- If you use string pointer types, such as
PChar , you need to make sure your pointer points to the data type you need, and the compiler cannot always verify this. - If you pass strings to var parameters, the type of the variable should be exactly the same. Now this can be more complicated if you have two types of strings.
- If you use
string as a convenient byte array buffer for storing arbitrary data instead of a variable containing text, this will not work as a UnicodeString . Make sure they are declared as RawByteString as a workaround. - In any case, when you work with string bytes, for example, when reading or writing to / from TStream, make sure that your code does not assume that
char lasts one byte.
Check out Delphi Unicode Migration for Mere Mortals for more tricks and tips on how to make this work. It is not as difficult as it seems, but it is also not trivial. Good luck
Mason wheeler
source share