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