bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi..

I want to decode/encode a binary files which are contain fix length and variable length record.

Below is my file format.
Ericsson AIN records are stored in an input file in 2048 byte blocks. +Every block is made of variable length records padded to make a 2048 +byte blocks. Every record is made of a fixed 180 byte part. Records may contain add +itional tagged fields making the total length variable.
I am not sure either the file format belongs to ASN.1 or not and might be somebody could guide me.

Could somebody guide me where i can get the sample program or any manual guide on reading binary file with variable length or ASN.r file format.

Thank you, Baharin

Replies are listed 'Best First'.
Re: How to read binary file with variable length in perl
by moritz (Cardinal) on May 05, 2010 at 08:30 UTC
    • open the file with the :raw mode. See also: perlopentut. Put the file handle in binmode.
    • read as many bytes as you need
    • use unpack to extract the records you need. See also: perlpacktut
    • If necessary, use read again to obtain variable length records.
    • close the file
    • ...
    • PROFIT
Re: How to read binary file with variable length in perl
by Anonymous Monk on May 05, 2010 at 09:13 UTC
      Hi...

      Thank you very much for your reply.

      Anyway, could somebody clarify either the format is ASN.1 or not because it is contain fix and variable length. Below some sample of the data:
      33 30 33 32 34 34 30 30 30 30 32 32 34 30 20 20 20 20 20 20 30 30 30 47 4c 4d 41 2f 4d 59 35 34 43 4e 41 31 2f 30 49 4e 42 47 4d 4f 20 49 41 42 31 49 20 20 9f a1 0b 00 9f be 00 02 00 16 9f be 01 81 0f 01 55 01 f0 83 18 02 20 10 00 10 00 00 00 01 9f be 19 81 09 00 01 10 60 13 00 13 13 00 9f be 1b 08 02 01 10 ff ff ff ff ff a1 06 82 04 04 00 00 00
        Why don't you just try different libraries that implement asn.1? If most or all of them can read your files, they probably are asn.1. If not, probably not.

        Did you read the advice given to you in your previous questions, such as How to use Convert::ASN1 ? Is there anything you didn't understand?

Re: How to read binary file with variable length in perl
by marto (Cardinal) on May 05, 2010 at 09:42 UTC

    As anonymous monk points out, you've asked similar questions many times, is there something fundamental you're not understanding?

      Apparently he's missing the fact that no one here is going to write his code for him . . . :)

      Update: As (correctly) pointed out in a msg, append "(at least not for free)" in there.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Hi...
        I am sorry about my question. I have never think that I want somebody to write any program for me.
        But, i raised a question here to solved my problem and get some guide from expert here on binary and ASN.1 file format.
        Anyway, thank you very much but please think positive. I will refer to my previos question or any related question about this.