Re: Reading binary/hex files (Delphi loadfromfile option?)


[ Delphi Forum ]

Posted by webmaster Guido on January 22, 2004 at 15:43:32:

In Reply to: Reading binary/hex files (Delphi loadfromfile option?) posted by Ashraf El-Sayed on January 19, 2004 at 13:56:33:

: Hello again. I need to display the entries of a hex file I have. When using the normal text files, I usually create a listbox, then use the "LoadFromFile" option to copy the strings into the listbox.items. Is there a similar Delphi procedure for reading/loading hex files? Thanks!
--------------------------

No, there is no similar Delphi procedure for reading hex files, in fact "binary files".

In fact, you can use LoadFromFile to read *any* type of file, but this is useless if it isn't a text file for two reasons:

1. LoadFromFile reads text "lines" into items, a line ending with a CR/LF (carriage return/ line feed) code. In a binary file, there might not be any CR/LF or maybe just one in every so many thousand bytes.

2. If you display the data in a listbox or a memo component, every byte is translated to it's corresponding ASCII character, which would give you a strange mix of unreadable stuff, not the hex-codes.

How to proceed then? In pseudo code:

Open the file
Until the end-of-file 
  Read a byte
  Translate the byte into its hexcode
  Add to a temporary string
  If tempstring has desired length (8,16,...) then
    Add tempstring to listbox, afterwards clear tempstring
If tempstring is not empty then 
  Add tempstring to listbox


Close the file

Related Articles and Replies:


[ Delphi Forum ]