function FindByHexStr(const AFile, AHexStr: string): Integer;
function HexStr2ChrStr(HexStr: string): string;
var
I: Integer;
S: string;
begin
for I := 1 to Length(HexStr) do
begin
S := S + HexStr[I];
if I mod 2 = 0 then
begin
Result := Result + Chr(StrToInt('$' + S));
S := '';
end;
end;
end;
var
MS: TMemoryStream;
FileSize: Integer;
Buffer: string;
begin
Result := -1;
MS := TMemoryStream.Create;
try
MS.LoadFromFile(AFile);
FileSize := MS.Size;
SetLength(Buffer, FileSize);
MS.Read(Buffer[1], FileSize);
try
Result := Pos(HexStr2ChrStr(AHexStr), Buffer) - 1;
except
// Error
end;
finally
MS.Free;
end;
end;
{
Usage:
Edit1.Text := IntToStr(FindByHexStr('C:\Test.exe', '4D5A500002'));
}
function GetHexStrByOffset(AFile: string; const AOffsetB, AOffsetE: Integer):
string;
var
MS: TMemoryStream;
I, FileSize: Integer;
Buffer: array of Byte;
begin
Result := '';
MS := TMemoryStream.Create;
try
MS.LoadFromFile(AFile);
FileSize := AOffsetE - AOffsetB + 1;
SetLength(Buffer, FileSize);
MS.Position := AOffsetB;
MS.Read(Buffer[0], FileSize);
try
for I := 0 to Pred(FileSize) do
begin
Result := Result + IntToHex(Buffer[I], 2);
end;
except
// Error
end;
finally
MS.Free;
end;
end;
{
Usage:
Memo1.Text := GetHexStrByOffset('C:\Test.exe', 5, 300);
}