Previously written that the method is good, but when the file is too large (in fact very small, also a few m), the corresponding array will be very large, and then there will be run-time errors, prompting memory exhaustion, so I was depressed for a long time.
Recently in the blog Traffic Records found in Baidu keyword "some very mischievous VBS program" (it seems that the young people are very like to play this mouth), so opened a long-lost Baidu search for a moment, found in the third. Appearing in the second is Baidu Hi, which is also a hoax program, but also encrypted. I tried to decrypt, but found that the encryption seems to be more than the last few days to decrypt the virus more abnormal, not fruit.
Although I did not decrypt that program (if a real master would, trouble pointing), but learned from the use of a VBS to read and write binary files, another way, namely Microsoft.xmldom+adodb. Stream. This method is much more efficient than the original one and seems to have no file size limit.
Copy Code code as follows:
Function readbinary (FileName)
Const adTypeBinary = 1
Dim Stream, XMLDOM, node
Set xmldom = CreateObject ("Microsoft.XMLDOM")
Set node = xmldom. createelement ("binary")
Node. DataType = "Bin.hex"
Set stream = CreateObject ("ADODB.") Stream ")
Stream. Type = adTypeBinary
Stream. Open
Stream. LoadFromFile FileName
Node. nodeTypedValue = stream. Read
Stream. Close
Set stream = Nothing
readbinary = node. Text
Set node = Nothing
Set xmldom = Nothing
End Function
The parameter of the function is the file path to read, and the return value is a string containing the hexadecimal value of the binary file.
Copy Code code as follows:
Sub writebinary (FileName, Buf)
Const adTypeBinary = 1
Const Adsavecreateoverwrite = 2
Dim Stream, XMLDOM, node
Set xmldom = CreateObject ("Microsoft.XMLDOM")
Set node = xmldom. createelement ("binary")
Node. DataType = "Bin.hex"
Node. Text = Buf
Set stream = CreateObject ("ADODB.") Stream ")
Stream. Type = adTypeBinary
Stream. Open
Stream.Write node. nodeTypedValue
Stream.savetofile FileName, Adsavecreateoverwrite
Stream. Close
Set stream = Nothing
Set node = Nothing
Set xmldom = Nothing
End Sub
The first parameter is the file path to be read, and the second is a string containing the hexadecimal value of the binary file. As for how to get the hexadecimal value of the binary file, the method is much, the simplest way of course is to use the FSO object to Readbinary function return value to the text file, and then Ctrl+c,ctrl+v. In fact, using the 16 tool is also very simple.
The others don't say much, and look at the MSDN reference documentation. Wrote an example program, which contains all the data of the Sky City Music, double click to generate the MP3 file in the Sky City and play it in the same folder.
Original: http://demon.tw/programming/vbs-binary-file-another.html