arraysdelphiintegerbyte

Delphi XE3 -> Integer to array of Bytes


I have a data structure:

data = array of integer;

I have filled it from an

source = array of byte;

with

data[x] := Source[offset] or (Source[offset + 1] shl 8) or
    (Source[offset + 2] shl 16) or (Source[offset + 3] shl 24);

after processing these blocks i have to bring them back to "bytes"...

any idea?


Solution

  • You can do this in a one-liner using Move.

    Move(source[0], dest[0], Length(source)*SizeOf(source[0]));
    

    If you need to perform a network/host byte order transformation, then you can run across the integer array after the Move.

    In the opposite direction you do it all in reverse.

    If you haven't got byte order issues then you might not actually need to convert to a byte array at all. It's possible that you can use the integer array as is. Remember that, without byte order issues, the memory layout of the byte and integer arrays are the same (which is why you are able to blit with Move).