溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

VBS中怎么把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串

發(fā)布時(shí)間:2021-08-12 10:54:58 來源:億速云 閱讀:164 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“VBS中怎么把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“VBS中怎么把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串”吧!

至少有三種以上辦法,可以把二進(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è)方法非常簡單明了,但是處理大數(shù)據(jù)流時(shí),比較慢。 
建議只用來處理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倍。建議用來處理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 沒有什么限制--除了物理內(nèi)存之外。這種處理方式是MultiByte方式的100倍!你可以用它來處理高達(dá)100MB的數(shù)據(jù)! 這種轉(zhuǎn)換方式,你也可以用來把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 
要存儲、獲取二進(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. 

感謝各位的閱讀,以上就是“VBS中怎么把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對VBS中怎么把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vbs
AI