delphi声明了ansi字符串的大小(delphi declaring size of ansi string)

编程入门 行业动态 更新时间:2024-10-25 08:16:35
delphi声明了ansi字符串的大小(delphi declaring size of ansi string)

它很容易定义一个大小为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.

最满意答案

你不需要定义一个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);

更多推荐

本文发布于:2023-07-23 00:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   大小   声明   ansi   delphi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!