Little Helper
Little Helper

Reputation: 2456

How to convert PAnsiChar to WideString or string?

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

Upvotes: 14

Views: 59111

Answers (4)

David Heffernan
David Heffernan

Reputation: 612964

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.

Upvotes: 16

robmil
robmil

Reputation: 312

var
  s: AnsiString;
  w: WideString;
  p: PAnsiChar;
...
  s := p;
  w := WideString(s);

Upvotes: 4

Wouter van Nifterick
Wouter van Nifterick

Reputation: 24086

s:PAnsiChar;

WideString(AnsiString(s));

Or on unicode Delphi's you probably want:

String(AnsiString(s));

Upvotes: 2

Look for StrPas function in docs.

Upvotes: 1

Related Questions