unpack data form binary string
Copy link to clipboard
Copied
Hello,
i need help for unpacking data from a binary string. I realized it with a php code:
list($empty,$size) = unpack( 'V', fread($socket,4) ); $theresult = fread($socket,$size);
But how can i do this in as3 ? I didn´t find a possibilty to encode "unsigned long (always 32 bit, little endian byte order)". Is there a option like unpack in as3 ?
Thanks for help,
Jojo
Copy link to clipboard
Copied
There is the ByteArray class. Exactly how you will get your data into it I'm not sure, but that is what you need.
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/utils/ByteArray.html
Copy link to clipboard
Copied
Hi,
thank your for helping me. I tried all reading methods but i get always a four-sided figure which is empty. This shows me that he does not correctly recognize the format or he doesn´t know it. because this i posted the exact php code to get a solution. Maybe i´m wrong with my socket connection but i think i would get no data back form the server if my socket connection is wrong. I will just continue to try.
Edit:
Ok know i get the correct lenght of my data which I expect but how i can show this as string, not the length:
var ba:ByteArray = new ByteArray();
socket.readBytes(ba);
var sba:String = ba[0];
sba shows me the correct length but how can i get this as string ? (socket is normal socket connection);
Thanks for your help so far.
best regards,
Jojo
Copy link to clipboard
Copied
I think you might need readMultiByte() or one of the other "read" methods. I'm never totally clear on how many bytes each of the different ones get. But the code could look something like this:
var ba:ByteArray = new ByteArray();
socket.readBytes(ba);
socket.position=0
var sba:String = ba.readMultiByte(2,"unicode");
Of course you will need to replace "unicode" with whatever scheme the information is encoded with.
I was just involved in another thread (http://forums.adobe.com/message/2525912#2525912) a discussions where Flash seems to not properly understand UTF-16LE, so I'm not sure if that will cause you any problem (or if it is even related to what you are trying to do.
Copy link to clipboard
Copied
Hello,
big thanks to you, now i get the data correctly. Because of the Bug, i get UTF 32 Bit Little Endian data, that is it´s only a bug with the 16bit LE.
Best regards,
Jojo

