Search code examples
delphiutf-8indy

DELPHI INDY - Can not find parameter to activate UTF8 encoding/decoding


I'm using Delphi 10.3 and have created a TIdTCPServer and a client. I found that Unicode characters like ÅÄÖ are changed when sent.

ContexClient.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8;
ContexClient.Connection.IOHandler.WriteLn('abcÅÄÖ');

In the code above, I tried to fix that with the DefStringEncoding property, but the compiler doesn't find IndyTextEncoding_UTF8.

I've no idea what to do to get it working.

Any idea?


Solution

  • Indy’s default encoding is US-ASCII. That can be changed at runtime using the GIdDefaultTextEncoding variable in Indy’s IdGlobal unit. You can set it to encUTF8, for instance.

    That is why you see non-ASCII characters being changed. Using the TIdIOHandler.DefStringEncoding property is one solution for that. IndyTextEncoding_UTF8 is declared in the IdGlobal unit. Make sure that unit is added to your uses clause.

    TIdIOHandler.WriteLn() itself also has an optional AByteEncoding input parameter. If it is nil, DefStringEncoding is used. If that is also nil, GIdDefaultTextEncoding is used.