Delphi UTF8String convert to AnsiString explicit and implicit conversions have different results

147 views Asked by At

I know UTF8String convert to AnsiString will cause data lost, but I found that explicit and implicit conversions are not the same.

The following code shows that explicit conversions from UTF8String to AnsiString still could show the emoji character and the length of the string is 6, while implicit conversions cause a data loss which I expected.

So why is that? Is there any documention or explaination of this behavior?

I tried under Delphi 11.3 and 12, both get the same results.

var
  nUTFStr: UTF8String;
  nAStr1, nAStr2: AnsiString;
begin
  nUTFStr := 'Hi';

  nAStr1 := AnsiString(nUTFStr);
  nAStr2 := nUTFStr;

  ShowMessage(Format('%s, %d | %s, %d', [nAStr1, Length(nAStr1), nAStr2, Length(nAStr2)]));
end;

Result:

enter image description here

0

There are 0 answers