Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

Import binary file and convert to HEX data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fan924 - 24 Sep 2007 20:54 GMT
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?
Joel - 25 Sep 2007 00:30 GMT
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

> 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?
Fan924 - 25 Sep 2007 18:44 GMT
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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.