What is the difference between WideChar and AnsiChar?

Daisetsu picture Daisetsu · Feb 9, 2011 · Viewed 9.5k times · Source

I'm upgrading some ancient (from 2003) Delphi code to Delphi Architect XE and I'm running into a few problems. I am getting a number of errors where there are incompatible types. These errors don't happen in Delphi 6 so I must assume that this is because things have been upgraded.

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

Answer

Cosmin Prund picture Cosmin Prund · Feb 9, 2011

The short: prior to Delphi 2009 the native string type in Delphi used to be ANSI CHAR: Each char in every string was represented as an 8 bit char. Starting with Delphi 2009 Delphi's strings became UNICODE, using the UTF-16 notation: Now the basic Char uses 16 bits of data (2 bytes), and you probably don't need to know much about the Unicode code points that are represented as two consecutive 16 bits chars.

The 8 bit chars are called "Ansi Chars". An PAnsiChar is a pointer to 8 bit chars. The 16 bit chars are called "Wide Chars". An PWideChar is a pointer to 16 bit chars. Delphi knows the difference and does well if it doesn't allow you to mix the two!

More info

Here's a popular link on Unicode: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets

You can find some more information on migrating Delphi to Unicode here: New White Paper: Delphi Unicode Migration for Mere Mortals

You may also search SO for "Delphi Unicode migration".