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)

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
There is no limit to rsbinarytostring-except physical memory. This approach is 100 times times the multibyte way! You can use it to handle up to 100MB of data! This conversion, you can also use to convert multibyte strings to string. The following method converts multibyte strings to 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
Third: Use ADODB.stream this way is more common: ' 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 ' 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. 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.