[VBS]转换二进制数据为字符串常用办法

类别:Asp 点击:0 评论:0 推荐:

至少有三种以上办法,可以把二进制数据(比如您从ASP的Request.BinaryRead方法得到的数据)转换为字符串。

第一种:使用VBS的MultiByte 方法

实例:

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) If LBinary>0 Then RS.Fields.Append "mBinary", adLongVarChar, LBinary RS.Open RS.AddNew RS("mBinary").AppendChunk Binary RS.Update RSBinaryToString = RS("mBinary") Else RSBinaryToString = "" End If End Function RSBinaryToString 没有什么限制--除了物理内存之外。

这种处理方式是MultiByte方式的100倍!

你可以用它来处理高达100MB的数据!

 

这种转换方式,你也可以用来把MultiByte strings转换为String。下面这个

方法把MultiByte strings转换为Binary:

Function MultiByteToBinary(MultiByte) '© 2000 Antonin Foller, http://www.pstruh.cz ' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY) ' Using recordset Dim RS, LMultiByte, Binary Const adLongVarBinary = 205 Set RS = CreateObject("ADODB.Recordset") LMultiByte = LenB(MultiByte) If LMultiByte>0 Then RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte RS.Open RS.AddNew RS("mBinary").AppendChunk MultiByte & ChrB(0) RS.Update Binary = RS("mBinary").GetChunk(LMultiByte) End If MultiByteToBinary = Binary End Function

第三种:使用ADODB.Stream

这种方式是比较常用的:

'Stream_BinaryToString Function '2003 Antonin Foller, http://www.pstruh.cz 'Binary - VT_UI1 | VT_ARRAY data To convert To a string 'CharSet - charset of the source binary data - default is "us-ascii" Function Stream_BinaryToString(Binary, CharSet) Const adTypeText = 2 Const adTypeBinary = 1 '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.

 

郑昀兼做翻译。转贴请注明出处。

本文地址:http://com.8s8s.com/it/it8981.htm