John
John

Reputation: 1913

Pointer to String conversion?

I am allocating the memory with GetMem (1028 bytes length), so I have an allocated Pointer.

Then I am reading the content and I know that there is e.g. 1028 bytes read. how can I cast pointer, or convert it to a string?

Should I null terminate the content of the memory prior to conversion?

Thanks!

Upvotes: 5

Views: 12930

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163277

Use SetString. Pass it a string variable, your pointer, and the string length (1028). Delphi strings are implicitly null-terminated, so the function will add that automatically (even if your buffer already has null bytes in it).

Better yet, set the length of the string and read your data directly into it instead of using an intermediary buffer. If you must use an intermediary buffer, you may as well use one that's statically sized to 1028 bytes instead of complicating your program with dynamic memory management.

Upvotes: 10

Related Questions