English 中文(简体)
WideChar和AnsiChar之间有什么区别?
原标题:What is the difference between WideChar and AnsiChar?

我正在将一些古老的(2003年)Delphi代码升级到DelphiArchitectXE,我遇到了一些问题。我在存在不兼容类型的地方遇到了许多错误。这些错误不会在Delphi6中发生,所以我必须假设这是因为事情已经升级了。

老实说,我不知道PAnsiChar和PWideChar之间的区别是什么,但Delphi肯定知道区别,不会让我编译。如果我知道差异是什么,也许我可以弄清楚该使用哪一个或如何解决这个问题。

最佳回答

缩写:在Delphi 2009之前,Delphi中的原生字符串类型曾经是ANSI CHAR:每个字符串中的每个字符都表示为8位字符。从Delphi 2009开始,Delphi的字符串变成了UNICODE,使用UTF-16表示法:现在基本的Char使用16位数据(2个字节),您可能不需要了解太多关于表示为两个连续16位字符的UNICODE代码点的信息。

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!

更多信息

以下是Unicode上的一个流行链接:每个软件开发人员绝对、积极地必须知道的关于Unicode和字符集的绝对最小值

您可以在此处找到有关将Delphi迁移到Unicode的更多信息:新白皮书:用于Mere Mortals的Delphi Unicode迁移

您也可以在SO中搜索“Delphi Unicode迁移”。

问题回答

几年前,Delphi中的默认字符类型从AnsiChar(表示ANSI字符的单字节变量)更改为WideCharchar类型现在是WideChar的别名,而不是AnsiChar,string类型现在是UnicodeString(Delphi传统字符串类型的UTF-16 Unicode版本)的别名,而不是AnsiStringPChar现在是PWideChar的别名,不是PAnsiChar

编译器本身可以处理很多转换,但也存在一些问题:

  1. If you re using string-pointer types, such as PChar, you need to make sure your pointer is pointing to the right type of data, and the compiler can t always verify this.
  2. If you re passing strings to var parameters, the variable type needs to be exactly the same. This can be more complicated now that you ve got two string types to deal with.
  3. If you re using string as a convenient byte-array buffer for holding arbitrary data instead of a variable that holds text, that won t work as a UnicodeString. Make sure those are declared as RawByteString as a workaround.
  4. Anyplace you re dealing with string byte lengths, for example when reading or writing to/from a TStream, make sure your code isn t assuming that a char is one byte long.

看看Delphi Unicode Migration for Mere Mortals了解更多关于如何实现这一点的技巧和建议。这并不像听起来那么难,但也不是小事。祝你好运!





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...