BIN格式文件,如何把它转换(用delphi)成ascII文件? (300分)

  • 主题发起人 主题发起人 flysand
  • 开始时间 开始时间
F

flysand

Unregistered / Unconfirmed
GUEST, unregistred user!
一个程控交换机输出的BIN格式文件(通话记录),
如何把它转换(用delphi)成ascII文件?
该文件下载:
http://www.flysand.com/20030701.rar
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
procedure Display(const S: string);
{ Private declarations }
public
{ Public declarations }
end;

type
TDisplayProc = procedure(const s: string) of object;
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure ShowBinary(var Data
Count: Cardinal
DispProc: TDisplayProc);
var
line: string[80];
i: Cardinal;
p: PChar;
nStr: string[4];
const
posStart = 1;
binStart = 7;
ascStart = 57;
HexChars: PChar = '0123456789ABCDEF';
begin
p := @Data;
line := '';
for i := 0 to Count - 1 do
begin
if (i mod 16) = 0 then
begin
if Length(line) > 0 then
DispProc(line);
FillChar(line, SizeOf(line), ' ');
line[0] := Chr(72);
nStr := Format('%4.4X', );
Move(nStr[1], line[posStart], Length(nStr));
line[posStart + 4] := ':';
end;
if p >= ' ' then
line[i mod 16 + ascStart] := p
else
line[i mod 16 + ascStart] := '.';
line[binStart + 3 * (i mod 16)] := HexChars[(Ord(p) shr 4) and $F];
line[binStart + 3 * (i mod 16) + 1] := HexChars[Ord(p) and $F];
end;
DispProc(line);
end;


procedure TForm1.Display(const S: string)

begin
Memo1.Lines.Add(S);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ms: TMemoryStream;
begin
if Opendialog1.Execute then
begin
ms := TMemoryStream.Create;
try
ms.LoadFromfile(OpenDialog1.FileName);
ShowBinary(ms.Memory^, ms.Size, Display);
finally
ms.Free
end;
end;

end;

end.
 
怎么输出是一堆乱码,和用记事本打开的是一样
 
把语言变成对应的汉字,难.
 
把文件映射到内存再转换

unit Unit1;

interface

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

type
TForm1 = class(TForm)
btnConvert: TButton;
btnOpenFile: TButton;
OpenDialog1: TOpenDialog;
procedure btnOpenFileClick(Sender: TObject);
procedure btnConvertClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FileName:String;
FFileHandle: THandle
// Handle to the open file.
FMapHandle: THandle
// Handle to a file-mapping object
FFileSize: integer
// Variable to hold the file size.
FData: Pbyte
// Pointer to the file's data when mapped.
PData: byte;
Count:integer;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnOpenFileClick(Sender: TObject);
begin
if Opendialog1.Execute then
FileName:=Opendialog1.FileName
else
FileName:='';
end;

procedure TForm1.btnConvertClick(Sender: TObject);
var
ResultFile:TextFile;
Index:Integer;
TmpStr:char;
const
HexChars: PChar = '0123456789ABCDEF';
begin
if not FileExists(FileName) then
raise Exception.Create(文件不存在.')
else
FFileHandle := FileOpen(FileName, fmOpenReadWrite);
if FFileHandle = INVALID_HANDLE_VALUE then
raise Exception.Create('打开文件失败');
try
FFileSize := GetFileSize(FFileHandle, nil);
FMapHandle := CreateFileMapping(FFileHandle, nil,PAGE_READWRITE, 0, 0, nil);
if FMapHandle = 0 then
raise Exception.Create('创建文件映射对象失败');
finally
CloseHandle(FFileHandle);
end;
try
FData := MapViewOfFile(FMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, FFileSize);
if FData = nil then
raise Exception.Create('映射文件视图到进程地址空间失败');
finally
CloseHandle(FMapHandle);
end;
if FData<>nil then
begin
try
AssignFile(ResultFile,'d:/Converted.txt');
ReWrite(ResultFile);
while count <= FFileSize - 1 do
begin
pdata := fdata^;
Index := (Pdata shr 4) and 15;
tmpStr:=HexChars[index];
Write(ResultFile,tmpStr);
Index:=Pdata and 15;
tmpStr:=HexChars[Index];
Write(ResultFile,tmpStr);
inc(fdata);
inc(count);
end;
finally
CloseFile(ResultFile);
showmessage('文件转换结束!');
end;
end;
end;

end.
 
搞定了吗?我也遇到同样的问题
 
不好意思,不是想回答这个问题,是因为发错的位置,
只好删了,加上这句话了,只好给楼up一个了。[:P]
 
用串口监视程序(PORTMON)监视,可以看到有规则的ASCII文本
 
?????????????????????
 

Similar threads

后退
顶部