whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

48 Visitors Online


 
...read a binary file and display the byte values as ASCII?
Autor: P. Below
Homepage: http://www.teamb.com
[ Print tip ]  

Tip Rating (40):  
     




type
  
TDisplayProc = procedure(const s: stringof object;

procedure ShowBinary(var Data; Count: Cardinal; DispProc: TDisplayProc);

implementation


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', [i]);
      Move(nStr[1], line[posStart], Length(nStr));
      line[posStart + 4] := ':';
    end;
    if p[i] >= ' ' then
      
line[i mod 16 + ascStart] := p[i]
    else
      
line[i mod 16 + ascStart] := '.';
    line[binStart + 3 * (i mod 16)]     := HexChars[(Ord(p[i]) shr 4) and $F];
    line[binStart + 3 * (i mod 16) + 1] := HexChars[Ord(p[i]) 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;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners