I’m trying to convert string using
Var
encode:ansistring;
begin
encode:=UTF8Encode('اختبار');
showmessage(encode);
end;
It’s working fine in Delphi 7
but in Delphi XE2 it’s send Text as question marks
Any suggestions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In your Delphi 7 code you probably wrote something like this:
This was fine in Delphi 7 where
stringis an alias forAnsiString. In XE2 the genericstringtype is now an alias forUnicodeStringwhich is UTF-16 encoded. That means that when the code above is compiled by XE2, the UTF-8 encoded buffer returned byUTF8Encodeis interpreted as UTF-16 encoded text. And that mismatch is what leads to your string full of question marks.So, if you just wrote
then you would have the same behaviour as for your Delphi 7 code.
However, this is not the way to do it in Unicode Delphi. Instead you should use the
UTF8Stringtype. This is defined asAnsiString(65001)which means a string of 8 bit character units with code page65001, i.e. the UTF-8 codepage. When you do this you don’t need to callUTF8Encodeat all since the encoding attached to the string type means that the compiler can generated code to convert the string. Now you would simply write:The principal reference for the Unicode aspects of Delphi 2009 and later is Marco Cantù’s white paper: Delphi and Unicode which I recommend that you read before proceeding.