What is the difference between Ansichar and char? [duplicate]

随声附和 提交于 2019-12-04 07:11:11

问题


I recently ran into this data type mismatch. This is I never saw before. I hope someone could explain what these are and how are they different.

Error I got was F2063. [DCC Error] E2010 Incompatible types: 'AnsiChar' and 'Char'


回答1:


Historically in Delphi, the Char type was effectively a synonym for the ANSIChar type. That is, a single byte representing a character from an ANSI codepage. NOTE: This is a simplification that ignores the complications arising from multibyte characters which could be encountered in an ANSI string but will suffice for this answer.

This corresponded with the fact that the String type was effectively a synonym for ANSIString.

In Delphi 2009 onward, this changed.

With Delphi 2009, the String and Char types became synonyms for UnicodeString (a WideString with additional capabilities) and WideChar, respectively, reflecting the transition to Unicode as the native format for string and character types. A WideChar is a 2 byte value representing a single character of Unicode (or one half of a surrogate pair).

Therefore, in versions of Delphi prior to Delphi 2009, the following two variables were of compatible types:

var
  ach: ANSIChar;
  ch: Char;         // Synonymous with ANSIChar

However, in Delphi 2009 and later the meaning of the "ch" declarations changes:

var
  ach: ANSIChar;
  ch: Char;         // Synonymous with WIDEChar

As a result, the ach and ch variables are no longer of compatible types.

i.e. the reason you are getting this error is that you have some code which has been declared with ANSIChar types and other code which is using values declared of type Char. When compiled with an old version of Delphi where Char = ANSIChar, the two sets of code are compatible, but in Delphi 2009 and later Char = WideChar and so the two types (Char and ANSIChar) are not compatible.



来源:https://stackoverflow.com/questions/26472257/what-is-the-difference-between-ansichar-and-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!