Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Delphi equivalent to C++'s memset?

Tags:

c++

delphi

Is sz an array of char, also what is the memset pointing to in the buffer? How can 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 the 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; 
like image 460
user734781 Avatar asked Dec 26 '22 02:12

user734781


1 Answers

The equivalent to memset is FillChar and fills a range of bytes with a byte value.

Since all bytes in the array sz is set when the byte order of data is reversed, this line can be removed.

The byte reversal can be simplified a little (replacing and $FF with a type restriction):

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 enclosing the assignment with Byte(), the compiler is told to skip range checking. A comparison of the generated assembly code (with range checking on) reveals that this also produces a 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 of populating the sz array, without the bitshifting routines:

sz[0] := PByte(@data)[3];
sz[1] := PByte(@data)[2];
sz[2] := PByte(@data)[1];
sz[3] := PByte(@data)[0];
like image 84
LU RD Avatar answered Jan 02 '23 15:01

LU RD