What is the difference between WideChar and AnsiChar?

I am updating some ancient (since 2003) Delphi code for Delphi Architect XE, and I have several problems. I get a number of errors when there are incompatible types. These errors do not occur in Delphi 6, so I must assume that this is because things have been updated.

I honestly don't know what the difference is between PAnsiChar and PWideChar, but Delphi probably knows the difference and won't let me compile. If I knew what the differences were, maybe I could figure out what to use or how to fix it.

+8
delphi widechar
source share
2 answers

In short: before Delphi 2009, Delphi used its own string type as ANSI CHAR: each char in each line was represented as 8 char bits. Since Delphi 2009, Delphi strings have become UNICODE using UTF-16 notation: now the base char uses 16 bits of data (2 bytes), and you probably don't need to know much about Unicode code points, which are represented as two consecutive 16 bit characters.

8-bit characters are called "Ansi Chars". PAnsiChar is a pointer to 8-bit characters. 16-bit characters are called Wide Characters. PWideChar is a pointer to 16-bit characters. Delphi knows the difference and is good if it doesn't let you mix two!

Additional Information

Here's a popular link to Unicode: Absolute Minimum Every software developer Absolutely, should know positively about Unicode and character sets

You can find more information about porting Delphi to Unicode here: New reference: Unicode migration to Delphi for mere mortals

You can also find SO for "Unicode Delphi migration".

+18
source share

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

+9
source share

All Articles