Author: Teun Spaans
How can I read a binary file?
How can I show a binary file in a memo field?
Answer:
Solve 1:
Why?
This article has been written in answer to an old request by ismael u, asking how
an executable can be loaded in a memo or rich memo field.
First a remark, executables should usually not be stored in a tmemo field, but
rather in some blob field. However, there are some occasions on which one would
like to view an executable. Studying (differences between) compiled executables
comes to mind.
I assume that Ismael means executable when he says exec, and the solution is rather
simple.
How?
Loading a an executable in a memo field basically comes down to 2 steps. The first
step is reading the file from disk and loading the file into memory, the second
step is showing the loaded contents in the tmemo field.
The first step, reading the file from disk and loading it into memory, is rather
easy. Perhaps TFileStream could be used, but I prefer the rather low level FileOpen
function because of its performance. Also, when working with binary files, we must
keep in mind that these files may contain #0 and many pointer based operations
regard this as an end/of/string character.
Basically, here is the code, mostly a copy of the delphi5 help after fixing some
minor bugs. Just create a form, add a button and a fileopendialog,
1 procedure TForm1.Button1Click(Sender: TObject);
2 var
3 iFileHandle: Integer;
4 iFileLength: Integer;
5 iBytesRead: Integer;
6 Buffer: PChar;
7 begin
8 opendialog1.filter := 'executables|*.exe';
9 if opendialog1.Execute then
10 begin
11 try
12 iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead or fmShareDenyNone);
13 if iFileHandle > 0 then
14 begin
15 iFileLength := FileSeek(iFileHandle, 0, 2);
16 FileSeek(iFileHandle, 0, 0);
17 Buffer := PChar(AllocMem(iFileLength + 2));
18 iBytesRead := FileRead(iFileHandle, Buffer^, iFileLength);
19 // note that ^ is missing in D5 help.
20 FileClose(iFileHandle);
21 end;
22 finally
23 FreeMem(Buffer);
24 end;
25 end;
26 end;
The second step again poses us some questions. As the contents of the binary file
will contain #0, how will we show them?
The first way is to convert the entire Buffer read above into a string and add this
string to the memo. Doing this causes no technical problem, but the memo shows just
a few characters. That's probably now what we want. The cause are the
aforementioned #0 characters.
The second way is to go through the Buffer bit by bit, and switch to a new line
whenever we encounter a #0. Doing so is easy, and reveals that an ordinary
executable contains lots of #0 characters.
The third and probably best way is to show all characters in a hexagonal notation.
27 procedure TForm1.Button1Click(Sender: TObject);
28 var
29 iFileHandle: Integer;
30 iFileLength: Integer;
31 iBytesRead: Integer;
32 Buffer: PChar;
33 i, linelength: integer;
34 s: string;
35 line: string;
36 c: char;
37 ordval, ordval1, ordval2: integer;
38 begin
39 opendialog1.filter := 'executables|*.exe';
40 if opendialog1.Execute then
41 begin
42 try
43 iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead or fmShareDenyNone);
44 if iFileHandle > 0 then
45 begin
46 iFileLength := FileSeek(iFileHandle, 0, 2);
47 FileSeek(iFileHandle, 0, 0);
48 Buffer := PChar(AllocMem(iFileLength + 2));
49 iBytesRead := FileRead(iFileHandle, Buffer^, iFileLength);
50 // note that ^ is missing in D5 help.
51 // 3 ways of conversion and show:
52 // way 1: exe will contain \0 so this code shows only part of exe
53 memo1.lines.add('way 1*********************************************');
54 s := string(Buffer);
55 memo1.lines.add(s);
56 // way 2: use \0 as newline for purpose of displaying in memo1.
57 memo1.lines.add('way 2*********************************************');
58 LineLength := 0;
59 Line := '';
60 for i := 0 to iFileLength - 1 do
61 begin
62 if Buffer[i] = #0 then
63 begin
64 memo1.lines.add(Line);
65 LineLength := 0;
66 Line := '';
67 end
68 else
69 begin
70 inc(LineLength);
71 // perhaps provision should be added for LineLength > max
72 delphi stringlength
73 Line := Line + Buffer[i]; // memo1 will handle normal new line chars
74 end;
75 end;
76 // way 3: display every char as ord
77 memo1.lines.add('way 3*********************************************');
78 Line := '';
79 for i := 0 to iFileLength - 1 do
80 begin
81 c := Buffer[i];
82 ordval := ord(c);
83 ordval1 := ordval div 16;
84 ordval2 := ordval mod 16;
85 Line := Line + '0123456789ABCDEF'[ordval1 + 1] +
86 '0123456789ABCDEF'[ordval2 + 1];
87 if Length(Line) = 80 then
88 begin
89 memo1.lines.add(line);
90 line := '';
91 end;
92 end;
93 FileClose(iFileHandle);
94 end;
95 finally
96 FreeMem(Buffer);
97 end;
98 end;
99 end;
Solve 2:
There is an inbuild delphi function (which I think appears in pre delphi 5)
100
101 BinToHex(Buffer, Text: PChar; BufSize: Integer);
which would create an output buffer in hex format.
|