Import binary file and convert to HEX data

F

Fan924

I want to work with files made from a EPROM programmer. The file types
are binary and ASCII HEX. The HEX file I can import into excel with no
problem. I would like a macro that can convert the binary file to hex
data so I can work with it in excel. I searched the web but did not
find what I need. Too much non related info to sift through. I have
Excel 97. Any ideas?
 
J

Joel

This program writes the binary number 0 - 255 to a file. Then closes the
file and reads the file into the worksheet. I needed to write binary data so
I could test the read function. Data is stored in worksheet as two byte
character string. I had to format the wrok sheet as text before program
worked correctly. I can change the format if necessary to any format you
would like.

I didn't use the HEX function because it doesn't display the data as two
bytes. Number 0 to F with the hex function is only display as a single
character.

Sub BinaryStream()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s

Filename = "c:\temp\binary.txt"

Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile Filename 'Create a file

Set f = fs.GetFile(Filename)

Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
For Count = 0 To 255
ts.Write Chr(Count)
Next Count
ts.Close

Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
ColumnCount = 1
RowCount = 1
Do While ts.atendofstream = False
Hexbyte = Asc(ts.Read(1))
nibble = Int(Hexbyte / 16)
If nibble <= 9 Then
Hexupper = Chr(Asc("0") + nibble)
Else
Hexupper = Chr(Asc("A") + (nibble - 10))
End If
nibble = Hexbyte Mod 16
If nibble <= 9 Then
HEXlower = Chr(Asc("0") + nibble)
Else
HEXlower = Chr(Asc("A") + (nibble - 10))
End If
Cells(RowCount, ColumnCount) = _
Hexupper & HEXlower
ColumnCount = ColumnCount + 1
If ColumnCount > 16 Then
ColumnCount = 1
RowCount = RowCount + 1
End If
Loop

ts.Close
End Sub
 
F

Fan924

Hi Joel. Thanks a million. It worked great. I learned a lot just
trying to modity it for may application. Now, I am tryig to mofiy your
binary file read code to also read a hex file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top