3
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?

Arioch 'The
  • 15,799
  • 35
  • 62
taoxl
  • 125
  • 1
  • 1
  • 5
  • It is the correct way which works... You will have to be more specific with your example. What was your input and what did you get as output (or what do you feel you've lost). – TLama Jan 02 '14 at 09:50
  • There is also a chance that the loss already takes place during the assignment of `str2`. – Uwe Raabe Jan 02 '14 at 09:52
  • The intended way, that works without issuing compiler warnings, is the EXPLICIT typecast : `str1 := UnicodeString(str2);` You really have to tell more why you think some data was lost. `Writeln(length(str1),length(str2):20); if length(str1) = length(str2) then for I := 1 to length(str1) do writeln( str1[i] =str2[i]) ;` and see if there are False reported – Arioch 'The Jan 02 '14 at 10:04
  • @Arioch'The Your comparison loop is pretty naive and won't work outside Win-1252 code page, for all glyphs which maps the Unicode page 0. – Arnaud Bouchez Jan 02 '14 at 10:19
  • It will work in win866 as well for Cyrillics @arnaudbouchez – Arioch 'The Jan 02 '14 at 10:22

1 Answers1

4

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    This is, I believe, the answer to the question that you asked. However, you will probably not be satisfied with it. You maybe asked the wrong question. I suspect that you should have presented an SSCCE and asked why the string conversion behaved in a way that was unexpected to you. Such a question would be easy to answer and would surely solve your real problem. That would be a quite different question mind you. – David Heffernan Jan 02 '14 at 10:22
  • 1
    Ah. *That's* the problem. Stop reading binary data into a text data type. And *definitely* don't expect something sensible to occur when converting between multiple character encodings. Use a zip library to read the zip file. If you must load the data yourself, use a binary-safe data type like TBytes. – Rob Kennedy Jan 02 '14 at 16:05
  • @taoxl show please the code how you extract str2 from zip file. or if you meant you read zip file into string variable - that is "broken by design" idea, read zip file into `TBytes` or `TMemoryStream` or into other raw binary container. – Arioch 'The Jan 02 '14 at 18:48