c# - Array.copy 从 C# 到 Delphi

标签 c# arrays delphi

我有下一个 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/

相关文章:

c# - 在 ultragrid 中获取特定单元格值的条件

c# - MvvmCross 未安装

c# - 动态转换为通用接口(interface)

c# - 使用反射将对象数组转换为另一种类型的数组

dictionary - 使用 Delphi TDictionary 将字符串用作 case 语句的索引

c# - TCP 服务器和客户端 C# 最佳实践

JAVA-将数组返回到main

c - 在这个程序中,这一行是如何工作的 "flag[str[i]-' a']++;"有人能解释一下吗?

德尔福64位?

delphi - 在 Delphi 中裁剪并对齐插入的 BMP