title: Hex view of a file
body: <p>I am using Delphi 2009.</p>
  
  <p>I want to view the contents of a file (in hexadecimal) inside a memo.</p>
  
  <p>I'm using this code :</p>
  
  <pre><code>var
    Buffer:String;
  begin
    Buffer := '';
    AssignFile(sF,Source); //Assign file
    Reset(sF); 
    repeat
      Readln(sF,Buffer); //Load every line to a string.
      TempChar:=StrToHex(Buffer); //Convert to Hex using the function
      ...
    until EOF(sF);
  end;
  
  function StrToHex(AStr: string): string;
  var
  I ,Len: Integer;
  s: chr (0)..255;
  //s:byte;
  //s: char;
  begin
    len:=length(AStr);
    Result:='';
    for i:=1 to len  do
    begin
      s:=AStr[i];
  
      //The problem is here. Ord(s) is giving false values (251 instead of 255)
      //And in general the output differs from a professional hex editor.
  
      Result:=Result +' '+IntToHex(Ord(s),2)+'('+IntToStr(Ord(s))+')';
    end;
    Delete(Result,1,1);
  end; 
  </code></pre>
  
  <p></p>
  
  <p>When I declare variable "<strong>s</strong>" as <strong>char</strong> (i know that char goes up to 255) I get results   hex values up to 65535! </p>
  
  <p>When i declare variable "<strong>s</strong>" as <strong>byte</strong> or <strong>chr (0)..255</strong>, it outputs different hex values, comparing to any Hexadecimal Editor! </p>
  
  <p>Why is that? How can I see the correct values?</p>
  
  <p>Check images for the differences.</p>
  
  <p><strong>1st image</strong>: Professional Hex Editor.</p>
  
  <p><img src="https://i.stack.imgur.com/8lFW1.png" alt="Professional Hex Editor"></p>
  
  <p><strong>2nd image</strong>: Function output to Memo. </p>
  
  <p><img src="https://i.stack.imgur.com/F2Kie.png" alt="Marked value should be 255 (box character)"> </p>
  
  <p>Thank you.</p>