我有下一个 C# 代码。我需要将其转换为 Delphi,但我陷入了 C# 的 Array.copy 中。
也许我累了,没有看到明显的事情。 我在delphi中的数组复制函数总是返回空字节。
这是我正在尝试的:
C# code:
byte[] buffer = new byte[200];
byte[] tmpArray = new byte[16];
int lenToCopy = 4;
//fill buffer here
buffer=ReadBuffer();
Array.Copy(buffer, 2, tmpArray, 5, lenToCopy);
德尔福代码:
var lenToCopy:integer;
temparray, buffer:TBytes;
....
begin
lenToCopy := 4;
setlength(tmpArray,16);
fillchar(tmpArray[0],length(tmpArray),0);
buffer:=GetBuffer();// buffer is ok here
tmpArray:=ArrayCopy(buffer, 2, tmpArray, 5, lenToCopy); //here is the problem: I get an empty tmpArray result
end;
function ArrayCopy(src:TBytes;ixsource:integer;dest:TBytes;ixdest:integer;len:integer):TBytes;
begin
SetLength(result, len+ixdest);
Move(src[ixsource],dest[ixdest],len);
result:=dest;
end;
最佳答案
这个怎么样?
NewArray := Copy(OldArray, startIndex, Count);
如果您需要复制到数组的中间,您也可以这样做:
NewArray := 复制(TmpArray, 0, ixDest) + 复制(Buffer, 0, Len) + 复制(TmpArray,ixDest + Len,MaxInt);
当然,这不会有超强的性能,但它会起作用。
我只是凭空猜测...这也至少需要 Delphi 10 Seattle。
关于c# - Array.copy 从 C# 到 Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36758116/