Function SimpleBinaryToString(Binary) 'SimpleBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string) 'to a string (BSTR) using MultiByte VBS functions Dim I, S For I = 1 To LenB(Binary) S = S & Chr(AscB(MidB(Binary, I, 1))) Next SimpleBinaryToString = S End Function
这个方法非常简单明了,但是处理大数据流时,比较慢。 建议只用来处理100KB以下的数据。 下面的这个类似的方法,性能稍微好些: Function BinaryToString(Binary) 'Antonin Foller, http://www.pstruh.cz 'Optimized version of a simple BinaryToString algorithm.
Dim cl1, cl2, cl3, pl1, pl2, pl3 Dim L cl1 = 1 cl2 = 1 cl3 = 1 L = LenB(Binary)
Do While cl1<=L pl3 = pl3 & Chr(AscB(MidB(Binary,cl1,1))) cl1 = cl1 + 1 cl3 = cl3 + 1 If cl3>300 Then pl2 = pl2 & pl3 pl3 = "" cl3 = 1 cl2 = cl2 + 1 If cl2>200 Then pl1 = pl1 & pl2 pl2 = "" cl2 = 1 End If End If Loop BinaryToString = pl1 & pl2 & pl3 End Function BinaryToString方法比SimpleBinaryToString方法性能高20倍。建议用来处理2MB以下的数据。 第二种方法:使用ADODB.Recordset ADODB.Recordset 可以让你支持几乎所有VARIANT支持的数据类型,你可以用它在string和 binary之间转换。 Function RSBinaryToString(xBinary) 'Antonin Foller, http://www.pstruh.cz 'RSBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string) 'to a string (BSTR) using ADO recordset
Dim Binary 'MultiByte data must be converted To VT_UI1 | VT_ARRAY first. If vartype(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary
Dim RS, LBinary Const adLongVarChar = 201 Set RS = CreateObject("ADODB.Recordset") LBinary = LenB(Binary)
'Create Stream object Dim BinaryStream 'As New Stream Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data. BinaryStream.Type = adTypeBinary
'Open the stream And write text/string data To the object BinaryStream.Open BinaryStream.Write Binary
'Change stream type To binary BinaryStream.Position = 0 BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data. If Len(CharSet) > 0 Then BinaryStream.CharSet = CharSet Else BinaryStream.CharSet = "us-ascii" End If
'Open the stream And get binary data from the object Stream_BinaryToString = BinaryStream.ReadText End Function 要存储、获取二进制数据,从一个本地文件、上传的二进制数据文件或者ASP中,可以参考:Pure and Huge ASP file upload with progress.。 Tip keywords: Binary, Byte, Array, VT_UI1, VT_ARRAY, BinaryWrite, BinaryRead, ChrB, InstrB, LeftB, MidB, RightB, ASP, VBSCOPYRIGHT AND PERMITTED USE OF http://www.pstruh.cz/tips WEBSITE. The entire contents of PSTRUH Software website consist of copyright material owned by Antonin Foller, PSTRUH Software.