【问题标题】:How to handle UTF-8 and ANSI conversion before Delphi 2009?Delphi 2009 之前如何处理 UTF-8 和 ANSI 转换?
【发布时间】:2013-03-01 13:12:06
【问题描述】:

在 Delphi 2009 中,我们有:

RichEdit1.Lines.LoadFromFile(OpenDialog1.FileName,TEncoding.UTF8);
RichEdit1.Lines.SaveToFile(OpenDialog2.FileName,TEncoding.Unicode);    

如果我还没有TEconding,我该如何在 Delphi 2006 上执行此操作?

有没有办法把那个较新的图书馆运回那里?还是有隐藏在网络中的解决方案?

【问题讨论】:

  • 我相信 UTF8EncodeUTF8Decode 在 Delphi 2009 之前就已经存在。因此您可以手动解码/编码字节字符串。
  • 哇!伙计你真快!!发表你的答案,因为这是对的!!!问题解决了。
  • 这些函数无助于复制 TEncoding.Unicode,因为那是 UTF-16。
  • 一个标准的 RTF 文件只能包含 7 位 ASCII 字符 (en.wikipedia.org/wiki/Rich_Text_Format#Character_encoding),为什么要使用 UTF8?
  • 我制作了一个创建文本文件的安卓应用程序。但是那些是 UTF8 格式的。我在 delphi 中创建了另一个应用程序来读取这些 txt 文件并执行某些操作,但它们必须采用默认的 delphi 编码(我认为是 ANSI),为此我需要在打开它们时进行解码。

标签: delphi unicode utf-8 delphi-2009 delphi-2006


【解决方案1】:

我相信 UTF8EncodeUTF8Decode 甚至在 Delphi 2009 之前就已经存在。因此您可以手动解码/编码字节字符串。 (我自己做过。)

【讨论】:

    【解决方案2】:

    这是我的一个 Delphi 7 项目的 sn-p:

    function LoadFile(const fn: string): WideString;
    var
      f:TFileStream;
      src:AnsiString;
      wx:word;
      i,j:integer;
    begin
      if FileExists(fn) then
       begin
        f:=TFileStream.Create(fn,fmOpenRead or fmShareDenyNone);
        try
          f.Read(wx,2);
          if wx=$FEFF then
           begin
            //UTF16
            i:=(f.Size div 2)-1;
            SetLength(Result,i);
            f.Read(Result[1],i*2);
            //detect NULL's
            for j:=1 to i do if Result[j]=#0 then Result[j]:=' ';//?
           end
          else
           begin
            i:=0;
            if wx=$BBEF then f.Read(i,1);
            if (wx=$BBEF) and (i=$BF) then
             begin
              //UTF-8
              i:=f.Size-3;
              SetLength(src,i);
              f.Read(src[1],i);
              //detect NULL's
              for j:=1 to i do if src[j]=#0 then src[j]:=' ';//?
              Result:=UTF8Decode(src);
             end
            else
             begin
              //assume current encoding
              f.Position:=0;
              i:=f.Size;
              SetLength(src,i);
              f.Read(src[1],i);
              //detect NULL's
              for j:=1 to i do if src[j]=#0 then src[j]:=' ';//?
              Result:=src;
             end;
           end;
        finally
          f.Free;
        end;
       end
      else
        Result:='';
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2010-10-17
      • 2011-09-22
      • 2014-01-27
      • 1970-01-01
      • 2019-02-10
      相关资源
      最近更新 更多