PDA

View Full Version : Reading a binary file into Excel at certain hexadecimal addresses



trpkob
07-05-2012, 07:26 AM
I have a binary file with hexadecimal information stored within it at hexadecimal addresses. I would like to read in the contents of the file into Excel at specific addresses. For instance, if at 0x0000 there was 00, I would want to read that in and at other specific address. So far I have only been able to read in the entire file. Also, if ASCII information is readable I would want that to be read in instead of the binary. I would need to read in at certain addresses for a certain length. For example, at 0x204 I would want to read for a hex length of 4 generating E2 C1 EC F9 and at 0x208 I would also want to read a hex length of 4 generating 02 00 00 00. I want to store the contents and address in a separate cell. So column A would have the address and column B would have the contents. I have attached a screenshot of the file highlighting these areas. The code below is for reading the entire file which takes forever and sometimes freezes Excel; I only want to read in the data at certain addresses. Any suggestions on VBA code that could accomplish this?


Sub Button1_Click()
Dim intFileNum%, bytTemp As Byte, intCellRow
intFileNum = FreeFile
intCellRow = 0
Open "C:\Documents and Settings\...." For Binary Access Read As intFileNum
Do While Not EOF(intFileNum)
intCellRow = intCellRow + 1
Get intFileNum, , bytTemp
Cells(intCellRow, 1) = bytTemp
Loop
Close intFileNum
End Sub

Kenneth Hobs
07-05-2012, 07:47 AM
You were already given a solution in your other thread. Why start another?

http://www.vbaexpress.com/forum/showthread.php?t=42797

Your code is slow because each time you write to a cell, all cells get recalculated if autocalc is on and the the screen has to update. In the other thread, it writes all at once. For speedup methods, see my kb article.

http://vbaexpress.com/kb/getarticle.php?kb_id=1035

trpkob
07-05-2012, 07:58 AM
I am unable to get this working, the code reads the ASCII code contained at the end of the address and not the binary.


You were already given a solution in your other thread. Why start another?

http://www.vbaexpress.com/forum/showthread.php?t=42797

Your code is slow because each time you write to a cell, all cells get recalculated if autocalc is on and the the screen has to update. In the other thread, it writes all at once. For speedup methods, see my kb article.

http://vbaexpress.com/kb/getarticle.php?kb_id=1035