How to convert AnsiString to UnicodeString in Delphi XE4
Asked Answered
W

1

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?

Weatherly answered 2/1, 2014 at 9:47 Comment(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).Luckett
There is also a chance that the loss already takes place during the assignment of str2.Glider
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 reportedScalf
@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.Cavite
It will work in win866 as well for Cyrillics @arnaudbouchezScalf
I
5

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.

Inadvertence answered 2/1, 2014 at 10:8 Comment(3)
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.Inadvertence
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.Schnurr
@Weatherly 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.Scalf

© 2022 - 2024 — McMap. All rights reserved.