Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you assign a <code>UTF8String</code> variable to a <code>WideString</code> variable, the compiler automatically inserts instructions to decode the string (in Delphi 2009 and later). It coverts UTF-8 to UTF-16, which is what <code>WideString</code> holds. If your <code>WideString</code> variable holds Chinese characters, then that's because your UTF-8-encoded string holds UTF-8-encoded Chinese characters.</p> <p>If you want your string <code>ws</code> to hold 16-bit versions of the bytes in your <code>UTF8String</code> <code>s</code>, then you can by-pass the automatic conversion with some type-casting:</p> <pre><code>var ws: WideString; i: Integer; c: AnsiChar; SetLength(ws, Length(s)); for i := 1 to Length(s) do begin c := s[i]; ws[i] := WideChar(Ord(c)); end; </code></pre> <hr> <p>If you're using Delphi 2009 or later (which includes the XE series), then you should consider using <code>UnicodeString</code> instead of <code>WideString</code>. The former is a native Delphi type, whereas the latter is more of a wrapper for the Windows <code>BSTR</code> type. Both types exhibit the automatic conversion behavior when assigning to and from <code>AnsiString</code> derivatives like <code>UTF8String</code>, though, so they type you use doesn't affect this answer.</p> <hr> <p>In earlier Delphi versions, the compiler would attempt to decode the string using the system code page (which is never UTF-8). To make it decode the string properly, call <code>Utf8Decode</code>:</p> <pre><code>ws := Utf8Decode(s); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload