Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Accessing Bytes From Image Data In VBScript

Posted on 2003-03-14
7
Medium Priority
?
1,264 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Suggested Courses

580 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