How to convert AnsiString to UnicodeString in Delphi XE4

taoxl picture taoxl · Jan 2, 2014 · Viewed 18.4k times · Source
exzample code:
var
  str1 : String;
  str2 : AnsiString;
  ....
  str2 := ....;
  str1 := String(str2);  

I converted such as above,but it didn't work.i found that some data lost in str1.is there a safe way to convert AnsiString to UnicodeString?

Answer

David Heffernan picture David Heffernan · Jan 2, 2014

Your code is already correct. It will convert from ANSI to UTF-16 with no loss of information.

Thus I conclude that the information is lost when you assign to the AnsiString variable. In other words, the error in your code is contained in the .... part of your code.

The error will likely be that the data and the code page of your AnsiString variable do not match.