一个完整的程序代码: 文本文件---二进制文件之间的转换 //供参考 ( 积分: 0 )

  • 主题发起人 天河流星
  • 开始时间

天河流星

Unregistered / Unconfirmed
GUEST, unregistred user!
// 刚才应一个网友的要求, 写完的一个程序, 可供参考
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

procedure byte_to_hex(b : byte;
var b1,b2 : byte);
const
hext : string='0123456789ABCDEF';
begin
b1 := byte(hext[(b shr 4) +1]);
b2 := byte(hext[(b and $0f) +1]);
end;

procedure hex_to_byte(b1,b2 : byte;
var b : byte);
begin
if (b1 >= byte('A')) then
b := b1 - 55
else
b := b1 - 48;
b := b shl 4;
if (b2 >= byte('A')) then
b := b + b2 - 55
else
b := b + b2 - 48;
end;

procedure txt_bin(txtfname, binfname : string);
var ftxt,fbin : TFilestream;
i : integer;
b, b1, b2 : byte;
begin
ftxt := TFileStream.Create(txtfname, fmOpenread);
fbin := TFileStream.Create(binfname, fmCreate);
for i:=1 to ftxt.Sizedo
begin
ftxt.Read(b,1);
byte_to_hex(b,b1,b2);
fbin.write(b1,1);
fbin.write(b2,1);
end;
fbin.destroy;
ftxt.destroy;
end;

procedure bin_txt(binfname, txtfname : string);
var ftxt, fbin : TFileStream;
i : integer;
b, b1, b2 : byte;
begin
fbin := TFileStream.Create(binfname, fmOpenread);
ftxt := TFileStream.Create(txtfname, fmCreate);
for i:=1 to fbin.Size div 2do
begin
fbin.Read(b1, 1);
fbin.Read(b2, 1);
hex_to_byte(b1,b2,b);
ftxt.Write(b, 1);
end;
ftxt.Destroy ;
fbin.Destroy ;
end;


procedure TForm1.Button1Click(Sender: TObject);
var binfname : string;
begin
opendialog1.FileName := '*.TXT';
if opendialog1.Execute then
begin
if FileExists(opendialog1.FileName) then
begin
binfname := ExtractFileName(opendialog1.FileName);
binfname := ChangeFileExt(binfname, '.BIN');
txt_bin(opendialog1.FileName , binfname);
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var txtfname : string;
begin
opendialog1.FileName := '*.BIN';
if opendialog1.Execute then
begin
if FileExists(opendialog1.FileName) then
begin
txtfname := ExtractFileName(opendialog1.FileName);
txtfname := ChangeFileExt(txtfname, '.TXT');
bin_txt(opendialog1.FileName , txtfname);
end;
end;
end;

end.
 
// 刚才应一个网友的要求, 写完的一个程序, 可供参考
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

procedure byte_to_hex(b : byte;
var b1,b2 : byte);
const
hext : string='0123456789ABCDEF';
begin
b1 := byte(hext[(b shr 4) +1]);
b2 := byte(hext[(b and $0f) +1]);
end;

procedure hex_to_byte(b1,b2 : byte;
var b : byte);
begin
if (b1 >= byte('A')) then
b := b1 - 55
else
b := b1 - 48;
b := b shl 4;
if (b2 >= byte('A')) then
b := b + b2 - 55
else
b := b + b2 - 48;
end;

procedure txt_bin(txtfname, binfname : string);
var ftxt,fbin : TFilestream;
i : integer;
b, b1, b2 : byte;
begin
ftxt := TFileStream.Create(txtfname, fmOpenread);
fbin := TFileStream.Create(binfname, fmCreate);
for i:=1 to ftxt.Sizedo
begin
ftxt.Read(b,1);
byte_to_hex(b,b1,b2);
fbin.write(b1,1);
fbin.write(b2,1);
end;
fbin.destroy;
ftxt.destroy;
end;

procedure bin_txt(binfname, txtfname : string);
var ftxt, fbin : TFileStream;
i : integer;
b, b1, b2 : byte;
begin
fbin := TFileStream.Create(binfname, fmOpenread);
ftxt := TFileStream.Create(txtfname, fmCreate);
for i:=1 to fbin.Size div 2do
begin
fbin.Read(b1, 1);
fbin.Read(b2, 1);
hex_to_byte(b1,b2,b);
ftxt.Write(b, 1);
end;
ftxt.Destroy ;
fbin.Destroy ;
end;


procedure TForm1.Button1Click(Sender: TObject);
var binfname : string;
begin
opendialog1.FileName := '*.TXT';
if opendialog1.Execute then
begin
if FileExists(opendialog1.FileName) then
begin
binfname := ExtractFileName(opendialog1.FileName);
binfname := ChangeFileExt(binfname, '.BIN');
txt_bin(opendialog1.FileName , binfname);
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var txtfname : string;
begin
opendialog1.FileName := '*.BIN';
if opendialog1.Execute then
begin
if FileExists(opendialog1.FileName) then
begin
txtfname := ExtractFileName(opendialog1.FileName);
txtfname := ChangeFileExt(txtfname, '.TXT');
bin_txt(opendialog1.FileName , txtfname);
end;
end;
end;

end.
 
你的程序转文本成2进制没的问题 ,可以转2进制成文本 就有很多问题了 估计转不回原来的文本可能是乱码,
 
48与55代表什么意思?!
 
不错
不过不知道为什么转换成二进制文件后的文件大小扩大了一倍,我Txt大小是32.2K转换成二进制文件后就成了64.4K了 !!
 
顶部