如何使用 VBScript 读取二进制文件
How to read from a binary file using VBScript
我是 VBScript 的新手,如果我错了,请随时纠正我。
我在读取二进制文件时遇到问题。
我的需求是从二进制文件中读取最后四个字节并将其与局部变量进行比较。
但问题是我在将 ASCII 字符(最后 4 个字节)转换为等效的十六进制字符时得到了 0x3F
的值。但是我可以通过打开这个脚本创建的文件中看到有效数据,其中使用相同的二进制流写入文件。
我不确定我是否遗漏了什么
以下代码供参考
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.bin"
bootIOP = "C:\Bins.bin"
appIOP = "C:\Bins.bin"
bootDSP = "C:\Bins.bin"
codeDSP = "C:\Bins.bin"
dataDSP = "C:\Bins.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)
使用二进制数据而不是字符串数据时,您需要使用正确的函数变体。
LenB()
- Returns 二进制数据的字节长度。
MidB()
- Returns 来自二进制数据的指定字节数。
AscB()
- Returns一个字节的字符代码。
'Using the following pseudo code should help
hexstring = Hex(AscB(MidB(binarydata, start, numofbytes)))
有用的链接
我是 VBScript 的新手,如果我错了,请随时纠正我。
我在读取二进制文件时遇到问题。
我的需求是从二进制文件中读取最后四个字节并将其与局部变量进行比较。
但问题是我在将 ASCII 字符(最后 4 个字节)转换为等效的十六进制字符时得到了 0x3F
的值。但是我可以通过打开这个脚本创建的文件中看到有效数据,其中使用相同的二进制流写入文件。
我不确定我是否遗漏了什么
以下代码供参考
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.bin"
bootIOP = "C:\Bins.bin"
appIOP = "C:\Bins.bin"
bootDSP = "C:\Bins.bin"
codeDSP = "C:\Bins.bin"
dataDSP = "C:\Bins.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)
使用二进制数据而不是字符串数据时,您需要使用正确的函数变体。
LenB()
- Returns 二进制数据的字节长度。MidB()
- Returns 来自二进制数据的指定字节数。AscB()
- Returns一个字节的字符代码。
'Using the following pseudo code should help
hexstring = Hex(AscB(MidB(binarydata, start, numofbytes)))