' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Function ReadSmallInt(binf) ' only works with positive numbers
    buf = binf.Read(2)
    ReadSmallInt = 256 * AscB(MidB(buf, 2, 1)) + AscB(MidB(buf, 1, 1))
End Function

Function ReadLongInt(binf) ' only works with positive numbers
    buf = binf.Read(4)
    ReadLongInt = 256 * 256 * 256 * AscB(MidB(buf, 4, 1)) + 256 * 256 * AscB(MidB(buf, 3, 1)) + 256 * AscB(MidB(buf, 2, 1)) + AscB(MidB(buf, 1, 1))
End Function

Function BytesToString(buf, start, len)
  s = ""
  For i = 1 To len
    s = s & Chr(AscB(MidB(buf, start + i - 1, 1)))
  Next
  BytesToString = S
End Function

Function ReadString(binf, len)
    buf = binf.Read(1 + len)
    ReadString = BytesToString(buf, 2, AscB(MidB(buf, 1, 1)))
End Function


Set binf = CreateObject("ADODB.Stream")
binf.Type = 1 ' adTypeBinary
binf.Open
binf.LoadFromFile "test.dat"
Do While Not binf.EOS
    a = ReadSmallInt(binf)
    b = ReadLongInt(binf)
    c = ReadString(binf, 5)
    d1 = ReadString(binf, 10)
    d2 = ReadString(binf, 10)
    MsgBox CStr(a) & "," & CStr(b) & "," & c & "," & d1 & "," & d2
Loop
binf.Close
