delphi声明ansi字符串的大小
现在很容易定义一个大小为 3 的字符串(在旧的 delphi 代码中)
st:string[3];
,我们希望将代码移至 ansi
st:ansiString[3];
行不通!
对于高级 OEM 类型
st:oemString[3];
同样的问题,
type
OemString = Type AnsiString(CP_OEMCP);
如何在哪里声明固定长度的 Ansi 字符串和新的 OEM 类型?
更新:我知道它将创建一个固定长度的字符串。它是软件设计的一部分,旨在防止错误,并且对于程序至关重要。
Its easy to define a string at the size of 3 (in old delphi code)
st:string[3];
now, we wish to move the code to ansi
st:ansiString[3];
won't work!
and for adcanced oem type
st:oemString[3];
same problem, where
type
OemString = Type AnsiString(CP_OEMCP);
how could be declared a fixed length ansi string and the new oem type?
update: i know it will create a fixed length string. it is part of the design of the software to protect against mistakes, and is essential for the program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要定义 AnsiString 的大小。
该表示法
适用于 Pascal(和 Delphi 1)使用的短字符串,并且主要保留用于遗留目的。
短字符串的长度可以是 1 到 255 个字节。第一个(“隐藏”)字节包含长度。
AnsiString 是一个指向字符缓冲区的指针(0 终止)。它有一些内部魔力,比如引用计数。您可以安全地向现有字符串添加字符,因为编译器将为您处理所有令人讨厌的细节。
UnicodeStrings 类似于 AnsiStrings,但具有 unicode 字符(在本例中为 2 个字节)。现在默认字符串 (Delphi 2009) 映射到 UnicodeString。
AnsiString 类型有一个构造来添加代码页(用于定义 127 以上的字符),因此 CP_OEMCP:
You don't need to define the size of an AnsiString.
The notation
is for short strings used by Pascal (and Delphi 1) and it is mostly kept for legacy purposes.
Short strings can be 1 to 255 bytes long. The first ("hidden") byte contains the length.
AnsiString is a pointer to a character buffer (0 terminated). It has some internal magic like reference counting. And you can safely add characters to an existing string because the compiler will handle all the nasty details for you.
UnicodeStrings are like AnsiStrings, but with unicode chars (2 bytes in this case). The default string now (Delphi 2009) maps to UnicodeString.
the type AnsiString has a construct to add a codepage (used to define the characters above 127) hence the CP_OEMCP:
“短字符串”是“Ansi”字符串,因为它们仅可用于向后兼容 Delphi 之前的代码。
自 Delphi 2009 起,将始终使用当前 Ansi 代码页/字符集创建固定长度的“短字符串”。
但此类短字符串与所谓的
AnsiString
不同。没有短字符串的代码页。因为短字符串没有引用计数。代码页仅针对
AnsiString
类型存在,该类型不是固定长度,而是可变长度和引用计数,因此与string[.. 定义的短字符串完全不同的类型。 .]
。根据设计,您不能仅混合
Short String
和AnsiString
类型声明。两者都称为“字符串”,但类型不同。这是
Short String
的映射这是
AnsiString
或UnicodeString
类型的内存映射:这是
PSt: PAnsiChar
布局:因此,如果
AnsiString
和UnicodeString
类型之间存在一些相似之处,则short string
类型完全不同,并且可以不按你的意愿混合。"Short Strings" are "Ansi" String, because there are only available for backward compatibility of pre-Delphi code.
will always create a fixed-length "short string" with the current Ansi Code Page / Char Set, since Delphi 2009.
But such short strings are NOT the same than so called
AnsiString
. There is not code page for short strings. As there is no reference-count for short strings.The code page exists only for
AnsiString
type, which are not fixed-length, but variable-length, and reference counted, so a completely diverse type than a short string defined bystring[...]
.You can't just mix
Short String
andAnsiString
type declaration, by design. Both are called 'strings' but are diverse types.Here is the mapping of a
Short String
Here is the memory mapping of an
AnsiString
orUnicodeString
type:and here is the
PSt: PAnsiChar
layout:So if there is some similarities between
AnsiString
andUnicodeString
type, theshort string
type is totally diverse, and can't be mixed as you wished.仅当 Delphi 的 unicode 版本中的 String[3] 默认为 3 WideChars 时,这才有用。这会让我感到惊讶,但如果是这样,请使用:
That would only be usefull when String[3] in unicode versions of Delphi defaults to 3 WideChars. That would supprise me, but in case it is, use:
ansisstring 和 unicodestring 的大小会动态增长。编译器和运行时代码会为您处理所有这些事情。
请参阅:http://delphi.about.com/od/beginners/l/aa071800a .htm
有关更深入的说明,请参阅:http://www.codexterity.com/delphistrings.htm
长度可以是 1 个字符到 2GB 之间的任意值。
The size of an ansistring and unicodestring will grow dynamically. The compiler and runtime code handle all this stuff for you.
See: http://delphi.about.com/od/beginners/l/aa071800a.htm
For a more in depth explanation see: http://www.codexterity.com/delphistrings.htm
The length can be anything from 1 char to 2GB.
但是旧的 ShortString 类型,Delphi 中较新的字符串类型是动态的。它们根据需要增长和收缩。您可以调用 SetLength() 将字符串预分配给给定长度,如果您必须将数据逐段添加到您知道最终长度的字符串,这有助于避免重新分配内存,但即使在那之后,字符串仍然可以增长并且添加或删除数据时缩小。
如果需要静态字符串,可以使用字符数组[0..n],其大小不会动态更改。
But the old ShortString type, the newer string types in Delphi are dynamic. They grow and shrink as needed. You can preallocate a string to a given length calling SetLength(), useful to avoid re-allocating memory if you have to add data piece by piece to a string you know the final length anyway, but even after that the string can still grow and shrink when data are added or deleted.
If you need static strings you can use array[0..n] of chars, whose size won't change dynamically.