To Load File content as hex i use this code in Delphi 7 :
procedure ReadFileAsHex(const AFileName: string; ADestination: TStrings);
var fs: TFileStream;
buff: Byte;
linecount: Byte;
line: string;
begin
linecount := 0;
line := '';
fs := TFileStream.Create(AFileName, fmOpenRead);
try
while fs.Position < fs.Size do begin
fs.Read(buff, 1);
line := line + IntToHex(buff, 2) + ' ';
Inc(linecount);
if linecount = 16 then begin
ADestination.Add(line);
line := '';
linecount := 0;
end;
end;
if Length(line) <> 0 then
ADestination.Add(line);
finally
fs.Free;
end;
end;
This shows me Loaded File like this in Hex :
34 01 00 00 13 00 00 00 13 00 00 00 04 00 00 00
01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
34 01 00 00 13 00 00 00 13 00 00 00 04 00 00 00
01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
I want to Replace some data From actual file
For example I want to Replace Data From Offset(00000060) to Offset(00000070) for example with 00 all Is it possible or i will need some special component for this ?
Thanks
There's no such thing as "writing hex". Hex is just a way of representing a number and performing some math operations more easily. The hex value $00
is the exact same value as decimal 0
, and if written to a file as numbers they're the exact same thing. The same is also true if you write $FF
and decimal 255
; they end up in the file as the same value, written the same way.
In other words, writing either of the following variables to a file will result in the same exact file content:
const
DecimalZero = 0;
HexZero = $0;
The same can be said for these:
const
DecimalTwoFiftyFive = 255;
HexTwoFiftyFive = $FF;
You can tell you're actually reading numeric (non-text) values by the fact that the code you've posted has to use IntToHex
on the value to convert it to a hex string before it can add it to the line
variable, which is declared as a string
.
You're discussing writing binary (non-text) to the file, which simply means writing actual numeric values to the file rather than the textual representation of those numbers.
You just position the TFileStream
to the point where you want to start writing, and write the number of bytes you want to the file. You have to open the stream in write mode, instead of readonly as your code does using fmOpenRead
.
var
fs: TFileStream;
Buff: array of byte;
begin
// Set length of buffer and initialize buffer to zeros
SetLength(Buff, 10);
FillChar(Buff[0], Length(Buff), #0);
fs := TFileStream.Create(AFileName, fmOpenWrite);
try
fs.Position := 60; // Set to starting point of write
fs.Write(Buff[0], Length(Buff)); // Write bytes to file
finally
fs.Free;
end;
end;