PDA

View Full Version : Reading Binary Data


pulse00
06-06-2008, 10:44 AM
Hi all,

i'm trying to read a binary file containing a sequence of floats created in C.

Loading and reading the file works fine in php, but i can't get this to work
in actionscript:

Here's my approach:




in the constructor:

...

this.loader = new URLLoader();
this.loader.dataFormat = URLLoaderDataFormat.BINARY;
var bin = new URLRequest("http://path/to/some/binary/file");
this.loader.load(bin);
this.loader.addEventListener(Event.COMPLETE, completeHandler);

...



public function completeHandler() {

var data : ByteArray = loader.data;

while (data.bytesAvailable > 0 ) {

trace(data.readFloat);

}
}




However, the values i get are wrong. They are way too small compared
to the real ones. Around 1.9893951235729634e-38 as they should be
something like 0.234894.

Does anyone see something obvious i might be missing out ?


Thanks for your help.


-pulse00

DiamondDog
06-06-2008, 01:44 PM
I know nothing about binary files, and this may not be the complete
answer by any means, but aren't you missing a pair of brackets?

Shouldn't this:

public function completeHandler {

be this:

public function completeHandler( ) { ?

pulse00
06-06-2008, 02:51 PM
I know nothing about binary files, and this may not be the complete
answer by any means, but aren't you missing a pair of brackets?


absolutely right, thanks. was just a typo though - edited the post. Unfortunately the problem still remains.

lordofduct
06-06-2008, 03:54 PM
first off, it's probably a typo. That should be:
data.readFloat()

with out it you'll get an infinite loop, and that program won't really run (it'll shut down after 15 seconds).

Anyways... is it possible that the byteArray is compressed?

bowljoman
06-06-2008, 04:08 PM
Its likly BigEndian Byte array by default, and possibly LittleEndian File written By C

Try this before you do any read writes to data.


byteArray.endian=Endian.LittleEndian;


If that doesnt work, you may have to arrange some bitwise shifting.

pulse00
06-10-2008, 10:55 AM
Setting the endianness to Little_Endian resulted in getting slightly more
correct values.

I'm still not able to get the correct values, though. What confuses me is the
size of the ByteArray. The actual amount of float values in the
testfile is 9527. When reading the file in PHP, i can use a function called
'unpack' which returns an array containing the float values. The only
thing i pass to this function is the format of the binary data, and the actual
data of course.

The resulting array has exactly a size of 9527, the ByteArray in Flash
however has a size of 38108 - which corresponds to the file size, but not
the amount of float values.

What might be happening is that a binary representation of a signed float
takes 4 bytes (38108/9527 = 4), which would mean that for each float value
i have to read 4 bytes of the array and convert it into signed float.

Does this make sense? If yes, anyone knows how to accomplish this?

pulse00
06-10-2008, 11:10 AM
my bad, sorry. readFloat() does exactly what i described. after one call
to readFloat() bytesAvailable is 4 bytes smaller than before the call.

it works fine...