Lazarus

Using the Lazarus IDE => Designer => Topic started by: fiazhnd on June 13, 2017, 10:17:57 am

Title: Get Specific Hex value from Hex file
Post by: fiazhnd on June 13, 2017, 10:17:57 am
Can someone please explain to me in simple way how i get hex value from a file after open required data like this (a byte, word, Dword and hex value 8 byte long) from Hex offset 0x010 into edit1 and so on.
Title: Re: Get Specific Hex value from Hex file
Post by: Pascal on June 13, 2017, 10:28:26 am
Try SysUtils.Fomat:
Code: Pascal  [Select][+][-]
  1. hextringbyte := format('%x', [bytevar]);
  2. hexstringword := format('%x', [wordvar]);
  3. hexstringdword := format('%x', [dwordvar]);
Title: Re: Get Specific Hex value from Hex file
Post by: fiazhnd on June 13, 2017, 10:31:45 am
But How indicate the offset of hex file?
Title: Re: Get Specific Hex value from Hex file
Post by: Thaddy on June 13, 2017, 10:34:55 am
But How indicate the offset of hex file?
Use a filestream...? Sound like homework to me...

Btw: https://www.freepascal.org/docs-html/rtl/classes/bintohex.html
Title: Re: Get Specific Hex value from Hex file
Post by: fiazhnd on June 13, 2017, 10:59:55 am
Boss issue is simple if i know how to use filestream to extract value i wasn't asked at what i want if someone just writes small code to extract value from 0x10. that will be really helpful
Title: Re: Get Specific Hex value from Hex file
Post by: Thaddy on June 13, 2017, 11:05:22 am
Code: Pascal  [Select][+][-]
  1. program gotopos;
  2. {$mode objfpc}
  3. uses classes;
  4. var
  5.  s:TFilestream;
  6. begin
  7.  s:=TFilestream.Create('<yourfilename>', fmOpenRead);
  8.  try
  9.   s.Position := $10;
  10.   // whatever;
  11.  finally
  12.   s.free;
  13. end;
  14. end.
  15.  

But I would write code that tests:
- if the file exists
- if it is indeed bigger than 16 bytes
- etc...
Title: Re: Get Specific Hex value from Hex file
Post by: fiazhnd on June 13, 2017, 02:06:47 pm
Thank you Thaddy i think i am near what i wanted but still materially wrong  :-[

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2.  var
  3.  filename: string;
  4.  s:TFilestream;
  5. begin
  6.   if openDialog1.Execute then begin
  7.     filename := openDialog1.Filename;
  8.  s:=TFilestream.Create(filename, fmOpenRead);
  9.  try
  10.   s.Position := $10;
  11.   // whatever;
  12.  finally
  13.   Edit1.Text:=IntToStr($10);
  14.   s.free;
  15. end;
  16.   end;
  17. Closefile(Filename);
  18. end;    
Title: Re: Get Specific Hex value from Hex file
Post by: howardpc on June 13, 2017, 03:37:37 pm
You could use something like this
Code: Pascal  [Select][+][-]
  1. procedure ShowValuesAtHex10(const aFilename: string; const aMemo: TMemo);
  2. var
  3.   fs: TFileStream;
  4.   q: QWord;
  5.   d: DWord absolute q;
  6.   w: Word absolute q;
  7.   b: Byte absolute q;
  8. begin
  9.   if FileExists(aFilename) and (FileSize(aFilename) > $20)
  10.   and Assigned(aMemo) then begin
  11.     fs:=TFilestream.Create(aFilename, fmOpenRead);
  12.     try
  13.       fs.Position:=$10;
  14.       q:=fs.ReadQWord;
  15.       aMemo.Clear;
  16.       aMemo.Append(Format('Byte:  %x',[b]));
  17.       aMemo.Append(Format('Word:  %x',[w]));
  18.       aMemo.Append(Format('DWord: %x',[d]));
  19.       aMemo.Append(Format('QWord: %x',[q]));
  20.     finally
  21.       fs.free;
  22.     end;
  23.   end;
  24. end;

called using something like this:
Code: Pascal  [Select][+][-]
  1. if OpenDialog1.Execute then
  2.     ShowValuesAtHex10(OpenDialog1.FileName, Memo1);
Title: Re: Get Specific Hex value from Hex file
Post by: fiazhnd on June 13, 2017, 05:05:37 pm
Thank you, soo much work like a charm.
Title: Re: Get Specific Hex value from Hex file
Post by: fiazhnd on June 13, 2017, 07:53:53 pm
Mates, can i know why i received value of hex swap B8B479A4B9BF78FF instead of FF78BFB9A479B4B8 in Qword?
Title: Re: Get Specific Hex value from Hex file
Post by: howardpc on June 13, 2017, 08:04:17 pm
Google for endianness, bigendian, littleendian.
TinyPortal © 2005-2018