Trying to move project from Delphi 2007 to Delphi XE4. What is the best way to convert String to AnsiString in Delphi XE4?
- 1I fixed all the various mis-references to XE4. It's important to take care with stuff like this.David Heffernan– David Heffernan2013-12-05 14:39:26 +00:00Commented Dec 5, 2013 at 14:39
Add a comment |
1 Answer
You simply assign it:
var AnsiStr: AnsiString; Str: string; .... AnsiStr := Str; The compiler will emit a warning mind you:
W1058 Implicit string cast with potential data loss from 'string' to 'AnsiString'
You can use a cast to suppress that warning:
AnsiStr := AnsiString(Str); By default that gives no warning, although there is of course still potential for data loss. If you enable warning W1060 then the compiler says:
W1060 Explicit string cast with potential data loss from 'string' to 'AnsiString'
Of course, it's not expected that Delphi XE4 code has much place for the use of AnsiString. Unless you have a very specific interop requirement, then text is best held in the native data type, string. If you want to operate on byte arrays use TBytes or TArray<Byte>.
3 Comments
LU RD
Lots of serial communication protocols uses AnsiString. Being able to handle this in a sensible manner requires careful planning, since AnsiString D2009+ is not equal to AnsiString in D2007.
jep
I'm a little puzzled by this comment. Did you mean "String D2009+ is not equal to String in D2007"? Because if you actually declare something as AnsiString in both, I believe you'd not have any problems. Or did they actually change something in AnsiString as well?
Jens Fudge
They actually changed AnsiString in D2007 to a slightly different AnsiString in D2009