[英]Can not assign char array to string in Delphi 10.2
I have the following variable declarations: 我有以下变量声明:
arrChar_1: array[0..2] of Char;
arrChar_2: array[0..2] of Char;
str: string;
Then I made the assignment: 然后我做了作业:
str := arrChar_1 + arrChar_2;
This assignment works normally on Delphi 6. But error occurs when I compile it on Delphi 10.2: 这个赋值在Delphi 6上正常工作。但是当我在Delphi 10.2上编译它时会发生错误:
[dcc32 Error] MigrateConcatenateCharArray.dpr(26): E2008 Incompatible types
I'm solving this problem in the following way: 我正在通过以下方式解决这个问题:
str := Copy(first_arrChar, 0, StrLen(first_arrChar));
str := str + Copy(second_arrChar, 0, StrLen(second_arrChar));
Is there any other good solution to this problem? 这个问题有没有其他好的解决方案? (1)
(1)
In Delphi 6: 在Delphi 6中:
String = AnsiString
Char = AnsiChar
In Delphi 10.2: 在Delphi 10.2中:
String = UnicodeString
Char = WideChar
Can tell me what caused the incompatibility issue to occur? 可以告诉我是什么原因造成了不兼容问题? (2)
(2)
I'm understanding that widechar is a multi-byte character type. 我理解widechar是一个多字节字符类型。 Unicode is the way that characters are encoded.
Unicode是字符编码的方式。 But I'm confused about them.
但我对他们感到困惑。
The following compiles in all versions of Delphi: 以下编译在所有版本的Delphi中:
procedure Main;
var
arrChar_1: array[0..2] of AnsiChar;
arrChar_2: array[0..2] of AnsiChar;
str: AnsiString;
begin
str := arrChar_1 + arrChar_2;
end;
The following code does not compile in Unicode versions of Delphi: 以下代码不能在Unicode版本的Delphi中编译:
procedure Main;
var
arrChar_1: array[0..2] of WideChar;
arrChar_2: array[0..2] of WideChar;
str: UnicodeString;
begin
str := arrChar_1 + arrChar_2;
end;
This seems a little odd to me. 这对我来说有点奇怪。 Why should the concatenation operator be supported for
AnsiChar
arrays but not WideChar
arrays? 为什么连接运算符应该支持
AnsiChar
数组而不支持WideChar
数组?
If you examine how the concatenation operator is implemented for AnsiChar
arrays that begins to shed some light. 如果你检查如何为
AnsiChar
数组实现连接运算符,这些数组开始有所AnsiChar
。 The generated code first converts the arrays into ShortString
instances. 生成的代码首先将数组转换为
ShortString
实例。 These are then converted into Delphi AnsiString
instances. 然后将它们转换为Delphi
AnsiString
实例。 Finally the two AnsiString
instances are concatenated. 最后连接两个
AnsiString
实例。
Now, this would explain why the code fails for WideChar
arrays. 现在,这将解释为什么
WideChar
数组的代码失败。 The ShortString
type only supports AnsiChar
elements and so a different path through the string support routines would have been needed. ShortString
类型仅支持AnsiChar
元素,因此需要通过字符串支持例程的不同路径。 One can assume that the Embarcadero designers chose, for whatever reason, not to support this form of concatenation when implementing Unicode support. 可以假设Embarcadero设计者出于某种原因选择在实现Unicode支持时不支持这种形式的连接。
To back this idea up, consider the following: 为了支持这个想法,请考虑以下事项:
procedure Main;
var
arrChar_1: array[0..254] of AnsiChar;
arrChar_2: array[0..254] of AnsiChar;
str: AnsiString;
begin
str := arrChar_1 + arrChar_2;
end;
This compiles. 这编译。 But change either of the
254
upper bounds to 255
and the code fails to compile (in all versions of Delphi) reporting E2008 Incompatible types . 但是将
254
上限中的任何一个更改为255
并且代码无法编译(在所有版本的Delphi中)报告E2008不兼容的类型 。 That is because the array now exceeds the maximum length of a ShortString
object. 这是因为数组现在超过了
ShortString
对象的最大长度。
As for how to migrate your code to Unicode Delphi, I suggest that you simply cast the character arrays to string
: 至于如何将代码迁移到Unicode Delphi,我建议您只需将字符数组转换为
string
:
str := string(arrChar_1) + string(arrChar_2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.