Recent

Author Topic: Get Specific Hex value from Hex file  (Read 5854 times)

fiazhnd

  • New Member
  • *
  • Posts: 36
Get Specific Hex value from Hex file
« 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.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Get Specific Hex value from Hex file
« Reply #1 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]);
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: Get Specific Hex value from Hex file
« Reply #2 on: June 13, 2017, 10:31:45 am »
But How indicate the offset of hex file?

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Get Specific Hex value from Hex file
« Reply #3 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
Specialize a type, not a var.

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: Get Specific Hex value from Hex file
« Reply #4 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

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Get Specific Hex value from Hex file
« Reply #5 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...
« Last Edit: June 13, 2017, 11:10:06 am by Thaddy »
Specialize a type, not a var.

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: Get Specific Hex value from Hex file
« Reply #6 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;    

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Get Specific Hex value from Hex file
« Reply #7 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);

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: Get Specific Hex value from Hex file
« Reply #8 on: June 13, 2017, 05:05:37 pm »
Thank you, soo much work like a charm.

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: Get Specific Hex value from Hex file
« Reply #9 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?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Get Specific Hex value from Hex file
« Reply #10 on: June 13, 2017, 08:04:17 pm »
Google for endianness, bigendian, littleendian.

 

TinyPortal © 2005-2018