Board index » delphi » Re: Assigning individual bytes of a DWord

Re: Assigning individual bytes of a DWord


2004-09-02 05:48:23 PM
delphi9
Thanks Dale,
I'll have a try with your code and see what the resulting output is,
although the more I think about it, the more I think a straight typecast
would be more efficient as all it is doing is referencing a different address
in memory.
Dean
"Dale McKay" <XXXX@XXXXX.COM>writes
Quote
Hello Dean,

The following functions should do what you want.

type
TByteArray4 = packed array[1..4] of byte;

function ByteArray4ToDWORD( const PValue: TByteArray4 ): DWORD;
begin
Move( PValue, Result, SizeOf( Result ) );
end; //ByteArray4ToDWORD

function DWORDToByteArray4( const PValue: DWORD ): TByteArray4;
begin
Move( PValue, Result, Length( Result )*SizeOf( Result[Low(
Result )] ) );
end; //DWORDToByteArray4

example:

var
LTestArray : TByteArray4;
LDWordVal : DWORD;

// if LTestArray = {1,2,3,4};
LDWordVal := ByteArray4ToDWORD( LTestArray );
//LDWordVal =>67305985

ZeroMemory( @LTestArray, Length( LTestArray )*SizeOf( LTestArray[Low(
LTestArray )] ) );
//LTestArray =>{0,0,0,0}
LTestArray := DWORDToByteArray4( 67305985 );
//LTestArray =>{1,2,3,4}

regards
DM


 
 

Re: Assigning individual bytes of a DWord

Hi,
Quote
I have an array of 4 bytes that I need to push into a DWord variable:-

A : Array[1..4] of byte;
B : DWord;

Is there a simple way?
having read the other replies, one way that
hasn't been mentioned yet is:
procedure foo;
var
A : Array[1..4] of byte;
B : DWord ABSOLUTE A;
begin
B := $01020304;
(...)
results in A = (4,3,2,1). if that is what you want
to achieve, i can not think of a more efficient way
since A and B share the same memory location so
no records or typecasts needed...
( but it is certainly NOT OOP ;-) )
cheers,
Frank