I have a Delphi statement that I need to convert to C#, but I am not sure how the pointer dereferencing works in this circumstance.
var
myarray : array[0..15] of WORD8;
pBuf : PWORD32Buf;
begin
...
pBuf := @myarray;
Result := pBuf^[0] xor pBuf^[1] xor pBuf^[2] xor pBuf^[3];
end;
I understand that pBuf
points to myarray
and that pBuf^[0]
should therefore be the 32 bit value represented by the first 4 bytes of the array. But I am not clear what 4 bytes would be represented by pBuf^[1]
.
Would this be bytes 4 to 7 of myarray
?
You can use BitConverter.ToIn32 to acheive this:
byte[] myarray = new byte[16];
var result = BitConverter.ToInt32(myarray, 0) ^
BitConverter.ToInt32(myarray, 4) ^
BitConverter.ToInt32(myarray, 8) ^
BitConverter.ToInt32(myarray, 12) ;
If you want to make an unsigned number then use ToUInt32 rather than ToInt32
We can only guess at the answer, because we don't know what PWORD32Buf
is. Presumably it is a pointer to an array of DWORD
, where DWORD
is an unsigned 32 bit integer. And we also need to guess what WORD8
is, presumably an 8 byte type, probably unsigned.
In which case pBuf^[0]
is bytes 0 to 3, pBuf^[1]
is bytes 4 to 7, and so on. That would make sense because it would mean that all 16 bytes are included in the xor expression.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.