【问题标题】:Converting UTF8 to ANSI (ISO-8859-1) in Delphi在 Delphi 中将 UTF8 转换为 ANSI (ISO-8859-1)
【发布时间】:2016-02-10 08:36:05
【问题描述】:

我有一个关于必须将 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;

谢谢。

【问题讨论】:

  • 什么Delphi版本? - 最佳解决方案取决于它。添加适当的标签。
  • 您拥有的第一个字符串未显示为 UTF-8。它是 UTF-8 编码的字节被解释为别的东西,可能是 ISO-8859-1 或 Windows-1252。如果您首先将 UTF-8 字节解释为 UTF-8,那么您可能不会遇到这个问题。您应该调查的是Source 的来源以及错误的原因。
  • 这听起来很像你问错了问题并陷入了经典的 XY 问题。

标签: delphi utf-8 delphi-2010 ansistring


【解决方案1】:

除了我拥有的函数之外,我还解决了调用内部函数 UTF8toAnsi 的问题。我正在开发 Delphi 2010。

这样: utf8toAnsi(convertir_utf8_ansi(source));

【讨论】:

    【解决方案2】:

    如果您使用的是 Delphi 2009 或更高版本,您应该让 RTL 为您进行转换:

    type
      Latin1String = type AnsiString(28591); // codepage 28591 = ISO-8859-1
    var
      utf8: UTF8String;
      latin1: Latin1String;
    begin
      utf8 := ...; // your source UTF-8 string
      latin1 := Latin1String(utf8);
    end;
    

    如果您使用的是 Delphi 2007 或更早版本,您仍然可以进行转换,只需让操作系统为您完成:

    var
      utf8: UTF8String;
      latin1: AnsiString;
      ws: WideString;
      len: Integer;
    begin
      utf8 := ...; // your source UTF-8 string
      len := MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(utf8), Length(utf8), nil, 0);
      SetLength(ws, len);
      MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(utf8), Length(utf8), PWideChar(ws), len);
      len := WideCharToMultiByte(28591, 0, PWideChar(ws), Length(ws), nil, 0, nil, nil);
      SetLength(latin1, len);
      WideCharToMultiByte(28591, 0, PWideChar(ws), Length(ws), PAnsiChar(latin1), len, nil, nil);
    end;
    

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 2012-12-04
      • 2012-06-24
      相关资源
      最近更新 更多