// Jean-Pierre LESUEUR (@DarkCoderSc)
// ...
uses Winapi.Windows, System.Math, System.SysUtils, System.Classes;
// ...
{ _.BufferToHexView }
function BufferToHexView(const pBuffer : PVOID; const ABufferSize : UInt64) : String; overload;
var ARow : array of byte;
ABytesRead : UInt64;
x : Byte;
AStringBuilder : TStringBuilder;
AHexBuilder : TStringBuilder;
AAsciiBuilder : TStringBuilder;
function PrintChar(const AChar : Byte) : Char;
begin
if AChar in [32..126] then
result := Chr(AChar)
else
result := '.';
end;
const SPACE = #32;
begin
result := '';
///
AStringBuilder := TStringBuilder.Create();
AHexBuilder := TStringBuilder.Create(48);
AAsciiBuilder := TStringBuilder.Create(16);
try
ABytesRead := 0;
SetLength(ARow, 16);
repeat
if ABufferSize - ABytesRead < 16 then
SetLength(ARow, ABufferSize - ABytesRead);
///
CopyMemory(PByte(ARow), Pointer(NativeUInt(pBuffer) + ABytesRead), Length(ARow));
AHexBuilder.Clear();
AAsciiBuilder.Clear();
for x := 0 to Length(ARow) -1 do begin
AHexBuilder.Append(SPACE + IntToHex(ARow[x]));
AAsciiBuilder.Append(PrintChar(ARow[x]));
end;
AStringBuilder.AppendLine(
Format('%p:%p %-48s %s', [
Pointer(NativeUInt(pBuffer) + ABytesRead),
Pointer(ABytesRead),
AHexBuilder.ToString(),
AAsciiBuilder.ToString()
])
);
///
Inc(ABytesRead, Length(ARow));
until ABytesRead = ABufferSize;
///
result := AStringBuilder.ToString();
finally
if Assigned(AStringBuilder) then
FreeAndNil(AStringBuilder);
if Assigned(AHexBuilder) then
FreeAndNil(AHexBuilder);
if Assigned(AAsciiBuilder) then
FreeAndNil(AAsciiBuilder);
end;
end;
{ _.BufferToHexView }
function BufferToHexView(const AStream : TMemoryStream) : String; overload;
begin
result := '';
///
if not Assigned(AStream) or (AStream.Size = 0) then
Exit();
///
result := BufferToHexView(AStream.Memory, AStream.Size);
end;
{ _.BufferToHexView }
function BufferToHexView(const AFileName : TFileName) : String; overload;
var AFileStream : TFileStream;
AStream : TMemoryStream;
begin
AFileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
AStream := TMemoryStream.Create();
try
AStream.CopyFrom(AFileStream, AFileStream.Size);
///
result := BufferToHexView(AStream);
finally
if Assigned(AStream) then
FreeAndNil(AStream);
if Assigned(AFileStream) then
FreeAndNil(AFileStream);
end;
end;
// ...
// Example Usage
var AExample : array[0..1024] of byte;
I : Integer;
AExampleString : String;
AExampleInt64 : Int64;
AStream : TMemoryStream;
AFileName : TFileName;
begin
AllocConsole();
///
// Example with byte array
WriteLn('--- Byte Array ---');
for I := 0 to Length(AExample)-1 do
AExample[I] := I mod 256;
writeln(BufferToHexView(@AExample, length(AExample)));
// Example with Memory Stream
WriteLn('--- Memory Stream ---');
AStream := TMemoryStream.Create();
try
for I := 32 to 126 do
AStream.Write(I, SizeOf(Byte));
writeln(BufferToHexView(AStream));
finally
if Assigned(AStream) then
FreeAndNil(AStream);
end;
// Example with File Content
AFileName := 'c:\Windows\system.ini';
if FileExists(AFileName) then begin
WriteLn(Format('--- File (%s) ---', [AFileName]));
writeln(BufferToHexView(AFileName));
end;
// Example with String
WriteLn('--- String ---');
AExampleString := 'Hello, World';
writeln(BufferToHexView(PWideChar(AExampleString), Length(AExampleString) * SizeOf(WideChar)));
// Example with Int 64 (Signed)
WriteLn('--- Int 64 ---');
AExampleInt64 := 2801;
writeln(BufferToHexView(PInt64(@AExampleInt64), SizeOf(Int64)));
Readln;
// ...