Binary Object (single-byte character "array")

There is a certain ActiveX control I use that is capable of creating an object in the containing javascript or vbscript which is basically a single-byte character “array” or “string”. It is of course not actually either of those types of objects, because both javascript and vbscript seem to store strings with 2-byte characters, and arrays have at least the “length” property added to them.

Now, I actually have found a VBscript function that will take such a data structure (basically, a densely packed array of single bytes that each hold a printable-ascii-character code value) and return the unpacked, 2-byte character string representation (a readable string).

Function BinaryToString(Binary)
Dim I,S
For I = 1 to LenB(Binary)
S = S & Chr(AscB(MidB(Binary,I,1)))
Next
BinaryToString = S
End Function

Now, my goal is to create the same “binary object” from a Flash file, instead of having to use that other ActiveX control (basically I am trying to replace that other ActiveX control).

In AS3, I can create an identical type of byte structure using a ByteArray, in that the “in memory file” it works with is actually just a densely packed set of bytes, where each one represents a single ascii character (0-255 byte value). Of course, all the other info in the ByteArray data structure is unnecessary for my purposes (properties, methods, etc).

My problem is, I want to find out how to pass this object out of flash (using ExternalInterface obviously) and into the containing javascript or vbscript, in the same way that the ActiveX control I am referring to is able to create such a structure, but without having the “conversion” that happens at the AS-JS/VBS bridge mess everything up.

My strategy was: In AS, read from the byte stream of the ByteArray one byte at a time, and manually “pack” each set of 4 bytes into a 32-bit (4-byte) uint (by doing shift-lefts and bitwise &'s and such), and stick each uint into an array, and then pass that array to javascript or vbscript. then once i was in the external scripting container, i was hoping all that would be “added” was just a length parameter to represent it as an actual array object, and that I could simply ignore this set of bits/bytes and thus have my object as desired.

In fact, the object I get in the scripting container is in fact a valid, addressable array, with a length parameter added as expected (and with the correct value for the number of items!), but each item in the array has grown from the 4-byte representation I gave it when packing in single bytes to some larger number of bytes (the variant data type biting me in the butt!). In fact, different items in the array appear to be different sizes (in bytes), some 20 bytes, some 18 bytes, etc.

I check the size in bytes of a particular value (in javascript) by passing it to this simple VBscript function:

Function SizeOfBytes(myBlah)
Dim I
I = LenB(myBlah)
SizeOfBytes = I
End Function

The weird thing really is that the object’s values, plus it’s length property, do not constitute its entire byte size apparently. I sent down 20 packed uint values in my array, which collectively should have been 80 bytes in size (or 84 if you count the length property) and what I get is an array that is 430 bytes in size. I looped through all 20 items in the array (some 20, some 18, etc), and added up their sizes, and I get 392 bytes. And 4 bytes for the size of the length property. That only accounts for 396 bytes. So now I’m really confused as to where the other 34 bytes is going.

BTW, i’ve also been exploring another way of thinking about it, by sending the value out in its string form (creating the 2-byte string in javascript/vbscript), and then trying to pack it down to 1-byte values there… but no such luck thus far.

I still want to see if there’s any creative way of getting my “binary object” out of flash and into my script. Thoughts?