1

I want to take 4 Bytes from

Source : Array[0..500] of Byte;

where

c : integer; // Start Point

to

v : LongInt;

but

Move(Source[c], v, 4);

gives me only 1 Byte back. Where is my fault?

Thanks again.

4
  • 4
    There's nothing wrong with that code. BTW, I think Rudy's way is simpler: PLongint(@Source[c])^ Commented Nov 30, 2012 at 0:34
  • Works fine for me exactly as you have shown it. Make sure your c value is correct. Commented Nov 30, 2012 at 0:38
  • Move works as so many have said. In case you didn't understand my answer to your other question, use Move to copy then entire array. You don't need to do it element by element. Commented Nov 30, 2012 at 7:35
  • And yes, if you really just want a single integer, then use a pointer cast. Commented Nov 30, 2012 at 7:52

2 Answers 2

5

This source works perfectly fine. It may however look like it returns only a byte if only the first byte (the one at index c) contains a value other than 0.

This alternative, already suggested by Sertac Akyuz, works fine as well:

v := PLongInt(@Source[c])^;
Sign up to request clarification or add additional context in comments.

2 Comments

@Michael only god knows? it's a simple pointer dereference over a cast over a pointer obtained via @ (at) operator!
And the other way works either, as I demonstrate in my answer
2

I doubt move is failing:

Try this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  source: array[0..500] of Byte;
  C: Integer;
  V: LongInt;
begin
  source[0] := $55;
  source[1] := $55;
  source[2] := $55;
  source[3] := $55;
  C := 0;
  Move(Source[C], V, SizeOf(V));
  ShowMessage(IntToStr(V));
end;

You will see the number 1431655765 ($55555555) in the message.

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.