The common method of converting binary data to string in VBS _VBS
Source: Internet
Author: User
There are at least three ways to convert binary data, such as the data you get from the ASP's Request.BinaryRead method, to a string.
First: The multibyte method using the VBS
Instance:
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
This method is very simple and straightforward, but it is slower when dealing with large data streams.
It is recommended to process only data below 100KB.
Here's a similar approach with a slightly better performance:
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
The BinaryToString method is 20 times times higher than the Simplebinarytostring method. It is recommended to process data below 2MB.
The second method: using Adodb.recordset
ADODB. The Recordset allows you to support almost any variant-supported data type, and you can use it in string and
Conversion between 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.
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 ' 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 ' stream and get binary data from the object
stream_binarytostring = Binarystream.readtext
End Function
To store, get binary data, from a local file, uploaded binary data file or ASP, you can refer to: 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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.