[VBS] A common method for converting binary data into strings

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

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.