delphi 二进制转换为文本  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 二进制转换为文本


Test for a binary form file
//判断二进制格式文件
function IsDFMBinary(FileName: string): Boolean;
var
  F: TFileStream;
  B: Byte;
begin
  B := 0;
  F := TFileStream.Create(FileName, fmOpenRead);
  try
    F.Read( B, 1 );
    Result := B = $FF;
  finally
    F.Free;
  end;
end;
Convert from binary to text and vice versa
// 从二进制转换为文本,反之亦然
function Dfm2Txt(Src, Dest: string): boolean;
var
  SrcS, DestS: TFileStream;
begin
  if Src = Dest then
  begin
    MessageDlg('Error converting dfm file to binary!. '
      + 'The source file and destination file names are the same.',
      mtError, [mbOK], 0);
    result := False;
    exit;
  end;
  SrcS := TFileStream.Create(Src, fmOpenRead);
  DestS := TFileStream.Create(Dest, fmCreate);
  try
    ObjectResourceToText(SrcS, DestS);
    if FileExists(Src) and FileExists(Dest) then
      Result := True
    else
      Result := False;
  finally
    SrcS.Free;
    DestS.Free;
  end;
end;


function Txt2DFM(Src, Dest: string): boolean;
var
  SrcS, DestS: TFileStream;
begin
  if Src = Dest then
  begin
    MessageDlg('Error converting dfm file to binary!. '
      + 'The source file and destination file names are the same.',
      mtError, [mbOK], 0);
    Result := False;
    exit;
  end;
  SrcS := TFileStream.Create(Src, fmOpenRead);
  DestS := TFileStream.Create(Dest, fmCreate);
  try
    ObjectTextToResource(SrcS, DestS);
    if FileExists(Src) and FileExists(Dest) then
      Result := True
    else
      Result := False;
  finally
    SrcS.Free;
    DestS.Free;
  end;
end;
Open a Binary DFM File as a Text Stream
/// 打开二进制DFM文件作为文本流
function DfmFile2Stream(const Src: string; Dest: TStream): boolean;
var
  SrcS: TFileStream;
begin
  SrcS := TFileStream.Create(Src, fmOpenRead or fmShareDenyWrite);
  try
    ObjectResourceToText(SrcS, Dest);
    Result := True;
  finally
    SrcS.Free;
  end;
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.056210994720459 seconds