-
Mar 11th, 2024, 09:47 AM
#1
Thread Starter
Frenzied Member
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?
-
Mar 11th, 2024, 11:15 AM
#2
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
-
Mar 14th, 2024, 02:40 AM
#3
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
-
Mar 21st, 2024, 02:36 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|