Results 1 to 4 of 4

Thread: How do I use the byte arrays returned from Read method of the ADODB.Stream object?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    How do I use the byte arrays returned from Read method of the ADODB.Stream object?

    The only way in VBScript to read data properly from a binary file is the ADODB.Stream object. Here's some code I have to read a file in the VBScript code of my ASP page.
    Code:
    	dim obj
    	dim bytearray
    	dim n
    	set obj=createobject("adodb.stream")
    	obj.type=1
    	obj.open
    	obj.loadfromfile "mydata.dat"
    	bytearray = obj.read
    	obj.close
    			
    	for n=0 to 9
    		response.write(bytearray(n))
    		response.write(vbcrlf & "<br />")
    	next
    It reads a file called mydata.dat and attempts to output the text representation of the numerical values of the first 10 bytes of the byte array. But unlike other arrays, byte arrays don't seem to be able to be accessed like bytearray(n). Unlike in VB6, in VBScript the byte arrays very strongly resist being accessed in any way other than in bulk. I can pass the entire array variable around, but as soon as I attempt to access any of the bytes in that array, it suddenly gives me an error.

    What is the proper way to access the bytes of a byte array in VBScript?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,562

    Re: How do I use the byte arrays returned from Read method of the ADODB.Stream object

    Might help to post the error message you got. Otherwise we're just stumbling around in the dark.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,512

    Re: How do I use the byte arrays returned from Read method of the ADODB.Stream object

    adodb.stream.read returns a variant
    you assign it to bytearray which is a variant, too

    Why would you expect to have an array-access to it?

    Have you tried to read out the Sub-type of bytearray with VarType?
    https://www.w3schools.com/asp/func_vartype.asp
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,286

    Re: How do I use the byte arrays returned from Read method of the ADODB.Stream object

    The only way to read-out a Variant which contains a ByteArray:
    (where TypeName(VariantBytes) = "Byte()")
    ... is by converting the whole ByteBlob into a "non-WideChar-String" first:
    S = CStr(B)

    Here's complete code:
    (which requires the mydata.dat-file, to be 256 Bytes long, containing all Bytes from 0 to 255)...

    Code:
    Dim B
    With createobject("adodb.stream")
         .type=1
         .open
         .loadfromfile "c:\temp\mydata.dat"
        B = .read
    End With
    
    'MsgBox TypeName(B) 'shows Byte()
    
    Dim VBytes, i
    VBytes = ByteArrToVariantArr(B)
    For i = 0 To Ubound(VBytes)
        If VBytes(i) <> i Then Exit For
    Next
    If i <= Ubound(VBytes) Then
      MsgBox "Error in VBytes() at index: " & i
    Else
      MsgBox "All Byte-Values in VBytes are correct"
    End If
    
    Function ByteArrToVariantArr(B)
      Dim S, i, VArr()
      S = CStr(B) 'Convert the ByteArray into a "non-widechar-string"
      
      Redim VArr(Ubound(B)) 'redim the Output-Buffer
      
      'since S is "non-WideChar", we have to use the B-suffixed String-Functions in the loop
      For i = 0 To LenB(S)-1
        VArr(i) = AscB(MidB(S, i+1, 1))
      Next
    
      ByteArrToVariantArr = VArr
    End Function
    HTH

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width