Search code examples
delphifiremonkey

In firemonkey how to convert a unicode String to utf8 bytes?


In Firemonkey on mobile (so without any ansiString/UTF8String support) how to convert a unicode String to utf8 bytes array (Tbytes) ?


Solution

  • This is not related to FireMonkey in fact. Text encoding support is provided at the RTL level. You use the TEncoding class.

    To obtain UTF-8 bytes from a string, do:

    var
      bytes: TBytes;
      str: string;
    ....
    str := ...;
    bytes := TEncoding.UTF8.GetBytes(str);
    

    In the reverse direction:

    str := TEncoding.UTF8.GetString(bytes);