2

I have the next code in C#. I need to convert it to Delphi but I am stuck in Array.copy from C#.

Maybe I'm tired and are not seeing the obvious. My array copy function in delphi always return empty bytes.

Here is what I am trying:

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);

Delphi code:

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;
2
  • SetLength(dest, len+ixdest); Commented Apr 21, 2016 at 2:08
  • Dalija Prasnikar. Thank you. It is a good suggestion. Commented Apr 21, 2016 at 5:39

1 Answer 1

3

What about this?

NewArray := Copy(OldArray, startIndex, Count);

If you need to copy into the middle of an array, you could also do this:

NewArray := Copy(TmpArray, 0, ixDest) + Copy(Buffer, 0, Len) + Copy(TmpArray, ixDest + Len, MaxInt);

Of course that's not going to be super performant, but it will work.

I'm just shooting from the hip here... This also requires Delphi 10 Seattle at a minimum.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.