我有一个关于必须将 UTF8 字符串转换为 ANSI 字符串的代码的问题。我的代码适用于元音中的重音符号,但对于字母 Ñ 它不起作用。代码破坏了字符串。我该如何解决这个错误?
我在 UTF8 中
的字符串:EDIFICIO PEÃ'AS BLANCAS 如果正确
,我在 ANSI 中的字符串:EDIFICIO PEÑAS BLANCAS 我现在在 ANSI 中的字符串:EDIFICIO PE
代码在这里:
function TFormMain.convertir_utf8_ansi(const Source: string):string;
var
Iterator, SourceLength, FChar, NChar: Integer;
begin
Result := '';
Iterator := 0;
SourceLength := Length(Source);
while Iterator < SourceLength do
begin
Inc(Iterator);
FChar := Ord(Source[Iterator]);
if FChar >= $80 then
begin
Inc(Iterator);
if Iterator > SourceLength then break;
FChar := FChar and $3F;
if (FChar and $20) <> 0 then
begin
FChar := FChar and $1F;
NChar := Ord(Source[Iterator]);
if (NChar and $C0) <> $80 then break;
FChar := (FChar shl 6) or (NChar and $3F);
Inc(Iterator);
if Iterator > SourceLength then break;
end;
NChar := Ord(Source[Iterator]);
if (NChar and $C0) <> $80 then break;
Result := Result + WideChar((FChar shl 6) or (NChar and $3F));
end
else
Result := Result + WideChar(FChar);
end;
end;
谢谢。