Accessing Bytes From Image Data In VBScript

Using IIS 5 and SQL Server 2000 to retrieve data from a column of type "image" I can retrieve the data in VBScript:

  ' Get the (next) 10 bytes of data from the image field.
  aBuffer = .Fields( "Content" ).GetChunk( 10 )

TypeName( aBuffer ) returns "Byte()", LBound( aBuffer ) returns 0, and UBound( aBuffer ) returns 9.  It sounds like a well formed byte array with 10 elements.

The problem is that I need to convert the data into a string in order to do some processing.  If I retrieve the data in one byte chunks then I can collect them in a string:

  ' AscB will always convert the first byte of the argument.
  sContent = sContent & Chr( AscB( aBuffer ) )

I cannot access the individual array elements, e.g.

  ' Subscripting   aBuffer   doesn't work.
  sContent = sContent & Chr( AscB( aBuffer( 0 ) ) )

results in a "Type mismatch: 'aBuffer'" error.

  ' Convert the byte array to a string.
  sBuffer = CStr( aBuffer )

results in a string half the length of   aBuffer   filled with question marks (ASCII 63).  It looks like a failed conversion from Unicode to ASCII.

How can I access the array elements, or convert the entire byte array into a useful string, in VBScript?
unlikelyloginnameAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anderson22Commented:
VBScript doesn't support binary other than to stream it.  If you want this capability, write a VB activex com object that does the processing for you.

-rca
0
unlikelyloginnameAuthor Commented:
VBScript does support Byte as a subtype within Variant, but all variables are of type Variant.  Am I to understand that the issue is that GetChunk returns an array of Byte, but VBScript only "supports" an array of Variant?
0
anderson22Commented:
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

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

sybeCommented:
Here are 2 VBScript (ASP) functions that will convert Byte() to String. The last function can handle very big files quickly, but requires ADODB Version 2.5 or higher.

Function Byte2String(s)
    Dim i
    For i = 1 to LenB(s)
        Byte2String = Byte2String & CHR(AscB(MidB(s,i,1)))
    Next
End Function



Function RSBinaryToString(ByVal btBinary)
    Dim oRS, iLen
    Set oRS = Server.CreateObject("ADODB.Recordset")
    iLen = LenB(btBinary)

    If iLen > 0 Then
        oRS.Fields.Append "mBinary", 201, iLen
        oRS.Open
        oRS.AddNew
        oRS("mBinary").AppendChunk btBinary
        oRS.Update
        RSBinaryToString = oRS("mBinary")
    Else
        RSBinaryToString = ""
    End If
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
unlikelyloginnameAuthor Commented:
VBScript does support Byte as a subtype within Variant, but all variables are of type Variant.  Am I to understand that the issue is that GetChunk returns an array of Byte, but VBScript only "supports" an array of Variant?
0
unlikelyloginnameAuthor Commented:
VBScript does support Byte as a subtype within Variant, but all variables are of type Variant.  Am I to understand that the issue is that GetChunk returns an array of Byte, but VBScript only "supports" an array of Variant?
0
anderson22Commented:
Regards,

VBScript does not support binary.  It doesn't even support strong types (subtype?), although it will pass data around without losing integrity.  Thus, sybe has shown how to use an activex object to interpret vbscript data.

To Sybe:
Good answer.  I have actually written an object in the past to get around this.  I will now know to use the ADO object instead.

-rca
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.