Convert a binary data (BinaryRead) to a string by VBS
ActiveX RegEdit.   ActiveX User account Manager   Pure-ASP Upload
Export MDB/DBF from ASP   Active LogFile   WebChecker   ActiveX/ASP Multi Dictionary object
 See 
 also 
 IISTracer, real-time IIS monitor and logging tool.
 Huge ASP file upload with progress bar. 



Do you like this article?
Please, rate it
and write review!
Rated:
by Aspin.com users
What do you think?
 Top messages
 12.6.2003 9:14:29 
 Download multiple files in one http request (nbsp;File & data transferVBScript)
 4.5.2002 9:16:43 
 Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook. (nbsp;ASP / ASP.NetWSHVBScriptEmail)
 31.12.2000 18:29:16 
 Automatic file upload using IE+ADO without user interaction - VBSscript (nbsp;WSHFile & data transferVBScript)
 10.2.2005 
 Add an email account to a windows 2003 pop3 service using script (nbsp;VBAEmail)

 Convert a binary data (BinaryRead) to a string by VBS 

 Areas>Languages>VBScript
 Areas>ASP / ASP.Net>Functions>Conversion

      To work with real binary data (Safearray of bytes, VT_UI1 | VT_ARRAY ) see ByteArray class of ScriptUtilities library. ByteArray class lets you prepare binary data, save/restore the data from a disk, do conversion between binary and string data using several code pages (ansi, oem, utf8, …)

      There are at least two ways you can convert binary data (for example data from BinaryRead method) to a string.

1. Use VBS MultiByte functions :

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
      SimpleBinaryToString is clear function, but the function takes much time for large data. You can use it to convert data with up to 100kB of size (concatenation of large string takes much processor time). You can use similar function with better performance :
Function BinaryToString(Binary)
  'Antonin Foller, http://www.motobit.com
  '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
      This function is up to 20 times faster than SimpleBinaryToString. You can use it to convert up to 2 MB of binary data.

2. Use ADODB.Recordset :

      ADODB.Recordset lets you work with all supported VARIANT data types - also with binary and String data (VT_UI1 | VT_ARRAY, BSTR). It lets you convert between these two data formats :
Function RSBinaryToString(xBinary)
  'Antonin Foller, http://www.motobit.com
  '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 is not limitted by time - only by physical memory. The function is up to 100 times faster than MultiByte conversions - you can use it to convert up to 100MB data.
      The function lets you also convert MultiByte strings to String. Next function converts MultiByte string to binary data :
Function MultiByteToBinary(MultiByte)
  '© 2000 Antonin Foller, http://www.motobit.com
  ' 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

3. Use ADODB.Stream :

      ADODB.Stream lets you work with String and binary data type. It lets you also convert between these two data formats :
'Stream_BinaryToString Function
'2003 Antonin Foller, http://www.motobit.com
'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
    To save/retrieve binary data from a disk, binary file upload and download in ASP or better work with binary data (different code pages - ANSI, MAC, UTF, search binary data, etc.) see Pure and Huge ASP file upload with progress.
 

See also for 'Convert a binary data (BinaryRead) to a string by VBS' article:

     Convert data to another charset/codepage in vb net Short sample to convert String Unicode data to another charset/codepage using System.Text.Encoding
     Read and write SQL image data, store binary file to sql table. Store and read SQL image/binary data using functions in this article. You can store local or remote files in an sql table along with a description and other fields.
     Work with binary files in VBSscript - read and write local and remote files Reading and writting binary and text files is a first task you will need to solve in server-side ASP. This article contains several VBS functions which let's you store data to local disk and read local or remote (http) files.
     Post large form data to ASP - Request.Form and stack overflow error? This article shows a way to work with request.form fields with any size, without 'Stack overflow' error.
     Create and work with binary data in ASP/VBScript Lets you convert/create binary data in ASP to use the data for BinaryWrite/BinaryRead.

If you like this page, please include next link on your pages:
<A
 Href="http://www.motobit.com/tips/detpg_binarytostring/"
 Title="This article, demonstrates several versions of
  source VBS code you can
  use to work with binary
  data in ASP and convert
  the data to a String
  format."
>Convert a binary data (BinaryRead) to a string by VBS</A>

     IISTracer - IIS ISAPI real-time monitor IISTracer is a real-time monitoring tool for Microsoft IIS, which will show/log you what is happenning on IIS server right now. It let's you reveal problems with long-running scripts (.asp, .cgi, cfm...), hang-up states and low resource situations and lets you stop long-running requests (uploads/downloads).      ActiveX User account Manager - Set of simple objects for creating, deleting, and managing user accounts, groups, servers and domains in the Windows NT environment.
     Active log file - Hi-performance text file logging for ASP/VBS/VBA applications. Lets you create daily/weekly/monthly log files with variable number of logged values and extra timing and performance info.      ActiveX windows registry editor - Intuitive, easy to use COM interface to windows registry. Set of classes to read/enumerate/modify windows registry keys and values from ASP, VBS and T-SQL.
     ActiveX/ASP Multi Dictionary object - Free-threaded hi-speed dictionary algorithm with unique/nonunique keys (map/multimap). Connect to another dictionary object in the same process. Lock and Unlock methods to synchronize tasks (application scope). Share ASP Application/Session objects.      Export DBF/MDB from ASP - Conversion from recordset to MDB/DBF. Direct binary output of MDB or DBF files from ASP pages with one row of code.
     Pure-ASP upload - lets you upload files using Pure ASP VBS code (using multipart/form-data and input type=file).      ByteArray - Works with safearray binary data (VT_UI1 | VT_ARRAY) - save/restore binary data from disk, find, work with code pages, convert to string/hexstring(SQL).
     WebChecker - Checks http, https, ftp and gopher internet connections in regular intervals. Lets you monitor web site functionality (uptime). Enables restart or notification on problems.      HTTPLog ISAPI filter - Lets you log incomming/outgoing http header and document data to separate files. Monitor of IIS service input/output.

© 1996 – 2009 Antonin Foller, PSTRUH Software, e-mail help@pstruh.cz