I have The following code for now.
type
TByte4 = array[0..3] of Byte; // 32-bit
function CardinalToBytes(const Data: Cardinal): TByte4;
begin
Result[0] := (Data shr 24) and 255;
Result[1] := (Data shr 16) and 255;
Result[2] := (Data shr 8) and 255;
Result[3] := Data and 255;
end;
function BytesToCardinal(const Data: TByte4): Cardinal;
begin
Result := (Data[0] * 16777216) +
(Data[1] * 65536) +
(Data[2] * 256) +
Data[3];
end;
I am wondering if this is the fastest and most efficient way (I am sure it is not :) ). The catch is this has to work in compact framework under Delphi 2006 (don't even ask). I am using it in a TEA encryption algorithm that works with Ansi and Unicode versions of Delphi and also with .NET and .NET compact framework in Delphi 2006.
So no "Move" and similar functions that work with pointers are allowed (no i do not want unsafe code).
Edit:
I still haven't found a better way to do this. Some great suggestion were given, but they all fail in .NET. I am afraid I will not spend any more time on a dead road. VCL .NET is dead, so is CF in delphi I am afraid. So this will have to do for maintaining this project. I will stil wait for while if somebody proves me wrong and gets the code to compile in .NET
Edit2:
The solution was simple as it is in most cases. Somebody just had to look for the obvious solution. I just didn't know anymore that there is a BitConverter class in .NET