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;

相关阅读 >>

Delphi中message消息的使用方法

Delphi屏蔽webbrowser页面中传出来的声音

Delphi 在线程中运行窗体

Delphi firedac连接mysql的时候报错

Delphi xe 新功能试用:多种皮肤样式静、动态设置方法

Delphi 多关键词批量替换

Delphi 圆角panel

Delphi firedac fdconnection 事务

Delphi瞬间消除无用托盘图标(刷新托盘)

Delphi驱动开发研究第一篇--实现原理

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...