views:

624

answers:

2

I have a very large number of app to convert to Delphi 2009 and there are a number of external interfaces that return pAnsiChars. Does anyone have a quick and simple way to cast these back to pChars? There is a lot on string to pAnsiChar, but much I can find on the other way around.

+2  A: 

There is no way to "cast" a PAnsiChar to a PChar. PChar is Unicode in Delphi 2009. Ansi data cannot be simply casted to Unicode, and vice versa. You have to perform an actual data conversion. If you have a PAnsiChar pointer to some data, and want to put the data into a Unicode string, then assign the PAnsiChar data to an AnsiString first, and then assign the AnsiString to the Unicode string as needed. Likewise, if you need to pass a Unicode string to a PAnsiChar, you have to assign the data to an AnsiString first. There are articles on Embarcadero's and TeamB's blog sites that take about Delphi 2009 migration issues.

Remy Lebeau - TeamB
I was hopping there was a built in function to do this that I was missing. Now I have my own library of functions to do the conversions.
Robert McCabe
+2  A: 

Delphi 2009 has added a new string type called RawByteString. It is defined as:

type
  RawByteString = type AnsiString($ffff);

If you need to save binary data coming in as PAnsiString, you can use this. You should be able to use the RawByteString the way you used AnsiString previously.

However, the recommended long term solution is still to convert your programs to Unicode.

lkessler
You have to be careful with RawByteString. Just like other string types, RawByteString still has a codepage associated with it, and uses that codepage during assignments to/from Unicode strings. The only difference being that RawByteString can change codepage dynamically at runtime, whereas other string types do not. RawByteString is not meant to be used as a standalone string type. Its primary (and only recommended) use in function parameters that do not care what codepage is used for the source Ansi character data.
Remy Lebeau - TeamB
This is a upgrade project and the problem arises with code that talks to Windows (which expects and returns ANSI chars).
Robert McCabe