?
Solved

Accessing Bytes From Image Data In VBScript

Posted on 2003-03-14
7
Medium Priority
?
1,191 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:unlikelyloginname
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:anderson22
ID: 8138575
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
 

Author Comment

by:unlikelyloginname
ID: 8139285
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
 
LVL 4

Expert Comment

by:anderson22
ID: 8140026
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 28

Accepted Solution

by:
sybe earned 1000 total points
ID: 8140042
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
 

Author Comment

by:unlikelyloginname
ID: 8141201
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
 

Author Comment

by:unlikelyloginname
ID: 8148778
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
 
LVL 4

Expert Comment

by:anderson22
ID: 8149520
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

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question