Delphi 6 StringLIst 讀取DAT 問題

筆記

先檢查DAT 字元格式內容是否為 ucs-2 little endian 

解決方法先把在讀取前把他轉乘UTF8儲存後

從新load utf8的檔案進來

再去做後續切割等動作


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
 function ReadUCS2FileToUTF8(const FilePath: string): UTF8String;
var
  f: TFileStream;
  d: WideString;
begin
  f := TFileStream.Create(FilePath, fmOpenRead or fmShareDenyWrite);
  try
    SetLength(d, f.Size div SizeOf(WideChar));
    if Length(d) > 0 then
      f.ReadBuffer(d[1], Length(d) * SizeOf(WideChar));
  finally
    f.Free;
  end;
  Result := UTF8Encode(d);
end;

procedure TForm1.Button1Click(Sender: TObject);
var  tmplist : TStringList;
I:Integer ;
S:UTF8String;
begin
   S:=ReadUCS2FileToUTF8('C:\1SP2G72701.dat');
  tmplist := TStringList.Create;
  tmplist.Add(S);
  tmplist.SaveToFile('C:\1SP2G72701.txt');


end;

end.

 

參考

http://stackoverflow.com/questions/35911059/delphi-6-convert-ucs-2-little-endian-file-to-utf8/35911816

 

 

 

 

大家一起加入blogads 賺零用錢!!