我有一个问题,要将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;
谢谢。
Source
来自哪里以及为什么出错了。 - Rob Kennedy