Difference between AnsiString, WideString, UnicodeString, ShortString and String and how to convert

5

While learning Delphi, I see implementations that use AnsiString , WideString , UnicodeString , ShortString , and String but do not know the difference between them.

Another thing that always occurs to me is that I try to save some type in another and it returns me the error that must be converted, however I always have difficulty doing it in the right way.

    
asked by anonymous 19.02.2015 / 21:18

1 answer

6
  • AnsiString : string composed of ASCII characters. Each Char has exactly 1 bytes. A pointer to a AnsiString ( ^AnsiString ) equals char* in C;

  • WideString : exists only for compatibility with Windows. Each Char has 2 bytes, and should be used in Win32 functions with parameters LPWSTR , casting PWChar ;

  • UnicodeString : string unicode. By default UTF-16 (or at least it was when I last searched), but can assume other encodings, such as UTF-8.

  • ShortString : equals the old Pascal string, with its 255 character limitation.

  • String : In newer versions of Delphi (2007 onwards), it is equivalent to UnicodeString . Formerly it was equal to AnsiString .

Both AnsiString and UnicodeString are more than a simple array of Char , and they have code page and size information. However, to facilitate casting of these types to PChar and its variations, this information is in the addresses previous to the one returned by the @ operator.

Conversion

Conversion between them is done automatically. Only care that should be taken is that data may be lost during conversion because the type does not support some feature of the source string.

For example, converting UnicodeString to AnsiString , there may be loss because Unicode characters can occupy more than 1 byte.

Conversion from AnsiString (or UnicodeString ) to ShortString , there will be data loss if the source string is greater than 255 ( Length(origem) > 255 ).

    
19.02.2015 / 21:31