What is Delphi equivalent C ++ memset?

Is sz an array of char and also what does memset point to the buffer? How do I convert the following C ++ code to Delphi?

int data = _ttoi(m_Strtag.GetBuffer(0));
unsigned char sz[4];
memset(sz,0, 4);
sz[0] = (unsigned char)((data >> 24) & 0xFF);
sz[1] = (unsigned char)((data >> 16) & 0xFF);
sz[2] = (unsigned char)((data >> 8) & 0xFF);
sz[3] = (unsigned char)(data & 0xFF);

      

This is a delphi call: if SAAT_YTagSelect (hp, isenable, 1, sz, 4) then ...

for the following delphi function:

function SAAT_YTagSelect(pHandle: Pointer; nOpEnable1, nMatchType: Byte; MatchData: PByte; nLenth: Byte): Boolean; stdcall; 

      

+3


source to share


1 answer


Equivalent to memset

FillChar and fills the byte range with the byte value.

Since all bytes in the array sz

are set when the byte order is data

reversed, this line can be removed.

Changing the byte can be simplified a little (replacing it and $FF

with a type constraint):

data := StrToInt(aString);
sz[0] := Byte(data shr 24);
sz[1] := Byte(data shr 16);
sz[2] := Byte(data shr 8);
sz[3] := Byte(data);

      




By attaching the assignment with Byte()

, the compiler is prompted to skip the range check. Comparing the generated assembly code (with range checking) shows that this also produces more efficient code:

Project1.dpr.36: sz[0] := Byte(data shr 24);   
0041C485 A1BC3E4200       mov eax,[$00423ebc]
0041C48A C1E818           shr eax,$18
0041C48D A2C03E4200       mov [$00423ec0],al


Project1.dpr.40: sz[0] := (data shr 24) and $FF;  
0041C485 A1BC3E4200       mov eax,[$00423ebc]
0041C48A C1E818           shr eax,$18
0041C48D 25FF000000       and eax,$000000ff
0041C492 3DFF000000       cmp eax,$000000ff
0041C497 7605             jbe $0041c49e
0041C499 E8F290FEFF       call @BoundErr
0041C49E A2C03E4200       mov [$00423ec0],al

      




A more direct way to populate an sz array without bitrate routines:

sz[0] := PByte(@data)[3];
sz[1] := PByte(@data)[2];
sz[2] := PByte(@data)[1];
sz[3] := PByte(@data)[0];

      

+8


source







All Articles