Search code examples
delphitype-conversionansi-c

Copying zero-terminated raw buffer of bytes to String


Sorry for all these questions I keep asking.Anyways my question is am I properly converting the value to a string?(Not an unincode string).

const
address:dword=$0057B568;
var
a:string;
len,i:dword;
begin
len:=0;
repeat
inc(len);
until ((pbyte(address+len)^=0));//and(pbyte(address+1)^=0));(for unincode)

for I:=0 to len do
a:=a+chr(pbyte(address+I)^);
//stringreplace(a,#0,'',[rfreplaceall,rfignorecase]);
MessageBox(0,pchar(a),'',0);
end.

Solution

  • No, it's not correct. The code is off by one byte. First, it assumes the string is at least one character long by ignoring the first byte. Next, it copies one extra byte. Your code can be greatly simplified:

    a := PAnsiChar(address);