主頁(yè) > 知識(shí)庫(kù) > VBS中轉(zhuǎn)換二進(jìn)制數(shù)據(jù)為字符串常用辦法

VBS中轉(zhuǎn)換二進(jìn)制數(shù)據(jù)為字符串常用辦法

熱門標(biāo)簽:外呼系統(tǒng)telrobot 百度地圖標(biāo)注是什么意思 怎么看地圖標(biāo)注 拉薩外呼系統(tǒng)業(yè)務(wù) 深圳外呼系統(tǒng)收費(fèi) 廣州ai電銷機(jī)器人一般多少錢 外呼系統(tǒng)免費(fèi)招代理 合肥高德地圖標(biāo)注 外呼線路中繼線是什么
至少有三種以上辦法,可以把二進(jìn)制數(shù)據(jù)(比如您從ASP的Request.BinaryRead方法得到的數(shù)據(jù))轉(zhuǎn)換為字符串。 

第一種:使用VBS的MultiByte 方法 

實(shí)例: 

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 

這個(gè)方法非常簡(jiǎn)單明了,但是處理大數(shù)據(jù)流時(shí),比較慢。 
建議只用來(lái)處理100KB以下的數(shù)據(jù)。 
下面的這個(gè)類似的方法,性能稍微好些: 
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倍。建議用來(lái)處理2MB以下的數(shù)據(jù)。 
第二種方法:使用ADODB.Recordset 
ADODB.Recordset 可以讓你支持幾乎所有VARIANT支持的數(shù)據(jù)類型,你可以用它在string和 
binary之間轉(zhuǎn)換。 
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 沒(méi)有什么限制--除了物理內(nèi)存之外。這種處理方式是MultiByte方式的100倍!你可以用它來(lái)處理高達(dá)100MB的數(shù)據(jù)! 這種轉(zhuǎn)換方式,你也可以用來(lái)把MultiByte strings轉(zhuǎn)換為String。下面這個(gè)方法把MultiByte strings轉(zhuǎn)換為Binary:Function MultiByteToBinary(MultiByte) 
'copy; 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 
要存儲(chǔ)、獲取二進(jìn)制數(shù)據(jù),從一個(gè)本地文件、上傳的二進(jìn)制數(shù)據(jù)文件或者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. 

標(biāo)簽:玉林 嘉興 周口 廣安 延安 臺(tái)灣 漳州 六安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《VBS中轉(zhuǎn)換二進(jìn)制數(shù)據(jù)為字符串常用辦法》,本文關(guān)鍵詞  VBS,中,轉(zhuǎn)換,二進(jìn)制,數(shù),據(jù)為,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《VBS中轉(zhuǎn)換二進(jìn)制數(shù)據(jù)為字符串常用辦法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于VBS中轉(zhuǎn)換二進(jìn)制數(shù)據(jù)為字符串常用辦法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章