in reply to Reading Binary file confusion


I think your template may not be right. You read 20 bytes with your first read, however, the template only unpacks 11 bytes of data.
print length pack 'H4C3H4b12H4'; # prints 11 H4 : 4 nybbles = 2 bytes C3 : 3 chars = 3 bytes H4 : 4 nybbles = 2 bytes b12: 12 bits!! = 2 bytes H4 : 4 nybbles = 2 bytes -- 11 bytes

But perhaps you wish to ignore the other 9 bytes in the header.

Unpacking the third field with "H4" gives you a hex string so you should probably use hex() to convert it to an integer. However, if it is a count of the blocks to follow then you could more easily unpack it with "v" or "n" depending on the endianness.

Also in your debug code you can use "H*" as a template for an unknown number of bytes.

--
John.

Replies are listed 'Best First'.
Re: Re: Reading Binary file confusion
by Anonymous Monk on Mar 12, 2002 at 00:15 UTC
    You are right, my template is off, but I will have to clarify this. However it seems to work out when it checks the header ID, and block size. The other 9 bytes may have been held for future development. I have to tramp through the history for this.

    For the third field I use "H4" because the data returned properly. I don't believe the number of blocks will never exceed 8 but I guess I should use the proper form anyway.

    Thanks for the tip on H*, this is my first attempt at Pack and unpack so my steps are a little unsteady still.