If you are using Delphi 2009 or later (Unicode versions), converting WideString to UTF8String is a simple assignment statement:
var ws: WideString; u8s: UTF8String; u8s := ws;
The compiler will call the desired library function to convert, because it knows that values โโof type UTF8String have a "code page" CP_UTF8 .
In Delphi 7 and later, you can use the provided library function Utf8Encode . For earlier versions, you can get this function from other libraries, such as JCL.
You can also write your own conversion function using the Windows API:
function CustomUtf8Encode(const ws: WideString): UTF8String; var n: Integer; begin n := WideCharToMultiByte(cp_UTF8, 0, PWideChar(ws), Length(ws), nil, 0, nil, nil); Win32Check(n <> 0); SetLength(Result, n); n := WideCharToMultiByte(cp_UTF8, 0, PWideChar(ws), Length(ws), PAnsiChar(Result), n, nil, nil); Win32Check(n = Length(Result)); end;
In most cases, you can just use UTF8String as an array, but if you really need a byte array, you can use the David and Cosmin functions. If you write your own character conversion function, you can skip UTF8String and go directly to the byte array; just change the return type to TBytes or array of Byte . (You might want to increase the length by one if you want the array to be completed with a value of zero. SetLength will do this implicitly in the string, but into the array.)
If you have another type of string that is neither WideString, UnicodeString, nor UTF8String, then the way to convert it to UTF-8 is to convert it to WideString or UnicodeString first and then convert back to UTF-8.