UTF8Encode or UTF16ToUTF8 for Unicode -> AnsiString? lazarus / 31-LCL ---------------------------------------------------------------------- Yakumo500 · 2011-10-14 18:21 Hi, i'm writing a text component and using the new UnicodeString type in fpc 2.7.1. For drawing i use ExtextOut function which needs a PAnsiChar. Should i use UTF8Encode or UTF16ToUTF8 to convert UnicodeString to AnsiString? ---------------------------------------------------------------------- felipemdc · 2011-10-14 18:28 UTF8Encode is part of the RTL and it uses the widestring manager, which with the existing widestring managers uses operating system routines UTF16ToUTF8 is in the LCL (although it will be copied to the new package LazUtils) and it is implemented purely in Pascal You can choose what you prefer. I like to use pure Pascal routines, because they execute the same code in every operating system, and therefore I consider them more reliable. ---------------------------------------------------------------------- Yakumo500 · 2011-10-14 19:23 If i use the following code with UTF8Encode and UTF16ToUTF8 i get different results: Code: [Select]var Str: UnicodeString; TextUtf8: AnsiString; //... Str := utf8toutf16('ä¶ 6'); TextUtf8 := UTF8Encode{Utf16ToUtf8}(Str); ExtTextOut(FDC, X, Y, ETO_CLIPPED, @Rect, PAnsiChar(TextUtf8), Length(TextUtf8), nil); If i use UTF8Encode everything is fine. But if i use Utf16ToUtf8 i get some weird result. I've attached a image with the results. ---------------------------------------------------------------------- felipemdc · 2011-10-15 05:05 In Free Pascal 2.4.4 it works correctly, so I am sure that these routines are correct. It simply looks like that you are getting a new automatic conversion somewhere in FPC 2.7.1, I don't recommend using FPC 2.7 together with Lazarus at the moment because Lazarus is waiting for the UTF-8 RTL. Also, notice that this is wrong in FPC 2.7: TextUtf8: AnsiString; It should be: TextUtf8: utf8string; ---------------------------------------------------------------------- Yakumo500 · 2011-10-15 08:44 Since which version exists UnicodeString?