2

I'm very new to VBScript, correct me at any point, if I'm wrong.

I'm facing issue while reading from a binary file.

My need is to read the last four bytes from a binary file and compare the same with a local variable.

But the issue is I'm getting value of 0x3F while converting the ASCII characters (of last 4 bytes) to Hex equivalent. But I'm able to see valid data in the files created by this script by opening, where the same binary stream is used to write to file.

I'm not sure if I'm missing something

Below is the code for reference

Const adTypeBinary = 1 
Const adSaveCreateOverWrite = 2 
Const CRC = 4
Dim BinaryStream, OutStream, StartPos, Bytes
'debug
Dim fso, MyFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
'debug
Dim bootstrapIOPCRC, bootIOPCRC, appIOPCRC, bootDSPCRC, codeDSPCRC, dataDSPCRC
Dim a(1)
bootstrapIOP = "C:\Bins\1.bin"
bootIOP = "C:\Bins\2.bin"
appIOP = "C:\Bins\3.bin"
bootDSP = "C:\Bins\4.bin"
codeDSP = "C:\Bins\5.bin"
dataDSP = "C:\Bins\6.bin"
'Create the BinaryStream object 
Set BinaryStream = CreateObject("ADODB.Stream") 
'Set it up and load in the source file 
BinaryStream.Type = adTypeBinary 
BinaryStream.Open 
' *** For bootstrapIOP
Bytes = 0
StartPos = 0
BinaryStream.LoadFromFile bootstrapIOP
'Create OutStream 
Set OutStream = CreateObject("ADODB.Stream") 
OutStream.Type = adTypeBinary 
OutStream.Open 
SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")
SET objFile = objFSO.GetFile(bootstrapIOP)
Bytes = CLng(objFile.Size)
StartPos = Bytes-CRC
'selecting the required data
BinaryStream.Position = StartPos 
BinaryStream.CopyTo OutStream, CRC
OutStream.SaveToFile "C:\bootstrapIOP.txt", adSaveCreateOverWrite 
bootstrapIOPCRC = 0
bootstrapIOPCRC = OutStream.Read ( CRC)
strHex =""
MsgBox((Asc(Mid(bootstrapIOPCRC,1,1))))
MsgBox(Mid(bootstrapIOPCRC,2,1))
MsgBox(bootstrapIOPCRC)
For i=1 To Len(bootstrapIOPCRC)
    strHex = strHex + Hex(Asc(Mid(bootstrapIOPCRC,i,1)))
Next
If (strHex = "") Then 
    MsgBox "Yippy"
Else 
    MsgBox(strHex)
End If
MsgBox(Len(bootstrapIOPCRC))
Set objFSO1=CreateObject("Scripting.FileSystemObject")
' How to write file
outFile="c:\test.txt"
Set objFile = objFSO1.CreateTextFile(outFile,True)
objFile.Write a(0)
objFile.Write a(1)
objFile.Close
' *** For bootIOP
Bytes = 0
StartPos = 0
BinaryStream.LoadFromFile bootIOP
SET objFile = objFSO.GetFile(bootIOP)
Bytes = CLng(objFile.Size)
StartPos = Bytes-CRC
'selecting the required data
BinaryStream.Position = StartPos 
BinaryStream.CopyTo OutStream, CRC
OutStream.SaveToFile "C:\bootIOP.txt", adSaveCreateOverWrite 
bootIOPCRC = 0
bootIOPCRC = OutStream.Read ( CRC)
' *** For appIOP
Bytes = 0
StartPos = 0
BinaryStream.LoadFromFile appIOP
SET objFile = objFSO.GetFile(appIOP)
Bytes = CLng(objFile.Size)
StartPos = Bytes-CRC
'selecting the required data
BinaryStream.Position = StartPos 
BinaryStream.CopyTo OutStream, CRC
OutStream.SaveToFile "C:\appIOP.txt", adSaveCreateOverWrite 
appIOPCRC = 0
appIOPCRC = OutStream.Read ( CRC)
' *** For bootDSP
Bytes = 0
StartPos = 0
BinaryStream.LoadFromFile bootDSP
SET objFile = objFSO.GetFile(bootDSP)
Bytes = CLng(objFile.Size)
StartPos = Bytes-CRC
'selecting the required data
BinaryStream.Position = StartPos 
BinaryStream.CopyTo OutStream, CRC
OutStream.SaveToFile "C:\bootDSP.txt", adSaveCreateOverWrite 
bootDSPCRC = 0
bootDSPCRC = OutStream.Read ( CRC)
' *** For codeDSP
Bytes = 0
StartPos = 0
BinaryStream.LoadFromFile codeDSP
SET objFile = objFSO.GetFile(codeDSP)
Bytes = CLng(objFile.Size)
StartPos = Bytes-CRC
'selecting the required data
BinaryStream.Position = StartPos 
BinaryStream.CopyTo OutStream, CRC
OutStream.SaveToFile "C:\codeDSP.txt", adSaveCreateOverWrite 
codeDSPCRC = 0
codeDSPCRC = OutStream.Read ( CRC)
' *** For dataDSP
Bytes = 0
StartPos = 0
BinaryStream.LoadFromFile dataDSP
SET objFile = objFSO.GetFile(dataDSP)
Bytes = CLng(objFile.Size)
StartPos = Bytes-CRC
'selecting the required data
BinaryStream.Position = StartPos 
BinaryStream.CopyTo OutStream, CRC
OutStream.SaveToFile "C:\dataDSP.txt", adSaveCreateOverWrite 
dataDSPCRC = 0
dataDSPCRC = OutStream.Read ( CRC)
2
  • 2
    aargh...my eyes!!! Maybe we start with making this code simpler. Have a look at this (msdn.microsoft.com/en-us/library/bx9ceb2w(v=vs.84).aspx) to prevent writing the same code over and over. Once it's simpler, it'll be easier to look at and debug. Reply if you need help getting this done. Also, try it for just one file first. Commented May 27, 2016 at 7:27
  • It will be the use of Mid() that is the problem should be using MidB() to return byte values, this is worth a read. Commented May 27, 2016 at 7:50

1 Answer 1

4

When working with binary data instead of string data you need to use the correct Function variations.

  • LenB() - Returns the length of binary data in bytes.
  • MidB() - Returns a specified number of bytes from binary data.
  • AscB() - Returns a character code for a byte.
'Using the following pseudo code should help
hexstring = Hex(AscB(MidB(binarydata, start, numofbytes)))

Useful Links

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Lankymart, using MidB solved my issue. Only issue is Hex(15) gives F, hence I added a check, as I need the data in 0x0F format for my application. Thanks once again
@sai Glad that's working for you, just as a side point maybe take on board what Ashwin mentioned. You have a lot of duplicated code which could be refactored, when possible it will make your life simpler following the DRY principle when it comes to coding.
I agree with you and Ashwin, that there is lot of repetitive code which can be made as a procedure and called, when required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.