我对将UTF8字符串转换为ANSI字符串的代码有疑问。我的代码适用于元音中的重音,但是带有字母Ñ则不起作用。该代码将字符串中断。我该如何解决这个错误?
我在UTF8中具有的字符串:EDIFICIOPEÃ'ASBLANCAS
如果正确,我将在ANSI中具有的字符串:EDIFICIOPEÑASBLANCAS
我现在在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;
谢谢。
除了我拥有的功能外,我还解决了调用内部函数UTF8toAnsi的问题。我正在研究Delphi 2010。
这样:Utf8toAnsi(convertir_utf8_ansi(source));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句