delphi声明ansi字符串的大小

发布于 2024-11-10 11:32:03 字数 415 浏览 4 评论 0原文

现在很容易定义一个大小为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄离 2024-11-17 11:32:03

您不需要定义 AnsiString 的大小。

该表示法

string[3] 

适用于 Pascal(和 Delphi 1)使用的短字符串,并且主要保留用于遗留目的。

短字符串的长度可以是 1 到 255 个字节。第一个(“隐藏”)字节包含长度。

AnsiString 是一个指向字符缓冲区的指针(0 终止)。它有一些内部魔力,比如引用计数。您可以安全地向现有字符串添加字符,因为编译器将为您处理所有令人讨厌的细节。

UnicodeStrings 类似于 AnsiStrings,但具有 unicode 字符(在本例中为 2 个字节)。现在默认字符串 (Delphi 2009) 映射到 UnicodeString。

AnsiString 类型有一个构造来添加代码页(用于定义 127 以上的字符),因此 CP_OEMCP:

OemString = Type AnsiString(CP_OEMCP);

You don't need to define the size of an AnsiString.

The notation

string[3] 

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:

OemString = Type AnsiString(CP_OEMCP);
过去的过去 2024-11-17 11:32:03

“短字符串”是“Ansi”字符串,因为它们仅可用于向后兼容 Delphi 之前的代码。

       st: string[3];

自 Delphi 2009 起,将始终使用当前 Ansi 代码页/字符集创建固定长度的“短字符串”。

但此类短字符串与所谓的 AnsiString 不同。没有短字符串的代码页。因为短字符串没有引用计数。

代码页仅针对 AnsiString 类型存在,该类型不是固定长度,而是可变长度和引用计数,因此与 string[.. 定义的短字符串完全不同的类型。 .]

根据设计,您不能仅混合 Short StringAnsiString 类型声明。两者都称为“字符串”,但类型不同。

这是 Short String 的映射

  st[0] = length(st)
  st[1] = 1st char (if any) in st
  st[2] = 2nd char (if any) in st
  st[3] = 3rd (if any) in st

这是 AnsiStringUnicodeString 类型的内存映射:

  st = nil   if st=''
  st = PAnsiChar if st<>''

这是 PSt: PAnsiChar 布局:

  PWord(PSt-12)^ = code page
  PWord(PSt-10)^ = reference count
  PInteger(PSt-8)^  = reference count
  PInteger(PSt-4)^  = length(st) in AnsiChar or UnicodeChar count
  PAnsiChar(PSt) / PWideChar(PSt) = Ansi or Unicode text stored in st, finished by a #0 char (AnsiChar or UnicodeChar)

因此,如果 AnsiStringUnicodeString 类型之间存在一些相似之处,则 short string 类型完全不同,并且可以不按你的意愿混合。

"Short Strings" are "Ansi" String, because there are only available for backward compatibility of pre-Delphi code.

       st: string[3];

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 by string[...].

You can't just mix Short String and AnsiString type declaration, by design. Both are called 'strings' but are diverse types.

Here is the mapping of a Short String

  st[0] = length(st)
  st[1] = 1st char (if any) in st
  st[2] = 2nd char (if any) in st
  st[3] = 3rd (if any) in st

Here is the memory mapping of an AnsiString or UnicodeString type:

  st = nil   if st=''
  st = PAnsiChar if st<>''

and here is the PSt: PAnsiChar layout:

  PWord(PSt-12)^ = code page
  PWord(PSt-10)^ = reference count
  PInteger(PSt-8)^  = reference count
  PInteger(PSt-4)^  = length(st) in AnsiChar or UnicodeChar count
  PAnsiChar(PSt) / PWideChar(PSt) = Ansi or Unicode text stored in st, finished by a #0 char (AnsiChar or UnicodeChar)

So if there is some similarities between AnsiString and UnicodeString type, the short string type is totally diverse, and can't be mixed as you wished.

如梦亦如幻 2024-11-17 11:32:03

仅当 Delphi 的 unicode 版本中的 String[3] 默认为 3 WideChars 时,这才有用。这会让我感到惊讶,但如果是这样,请使用:

st: array[1..3] of AnsiChar;

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:

st: array[1..3] of AnsiChar;
So尛奶瓶 2024-11-17 11:32:03

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.

一笑百媚生 2024-11-17 11:32:03

但是旧的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文