Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert PAnsiChar to WideString or string?

How do I convert a PAnsiChar variable to WideString or to string?

like image 853
Little Helper Avatar asked Apr 24 '11 07:04

Little Helper


1 Answers

You simply assign one variable to another and let the Delphi compiler do all the conversion for you:

var
  p: PAnsiChar;
  s: string;
  w: WideString;
....
s := p;
w := p;

If you want to convert in the other direction, and restricting the discussion to Delphi 7 for which Char, PChar, string are all ANSI data types you would use the following:

PAnsiChar(s);
PAnsiChar(AnsiString(w));

The casts are needed when going in this direction and in the case of the WideString the data must be explicitly converted from Unicode to ANSI before asking for a null-terminated C string pointer.

like image 79
David Heffernan Avatar answered Nov 08 '22 19:11

David Heffernan