Extraction of bits using unpack

Discussion in 'Perl Misc' started by pip, Aug 17, 2005.

  1. pip

    pip Guest

    Hi all,

    I am trying to extract some data from a binary file using unpack. All
    is well where I have to extract simple data types such as shorts,
    strings and ints but then I reach this in the docs I am using:

    Here is the way a date is represented:

    MSB LSB
    __________________________________________________________
    | year | Month | Day | Time |
    | 12 bits | 4 bits | 5 bits | 11 bits |
    |__________|____________|___________|____________________|

    I have played with the hex string operator in unpack with little
    joy. I also had a go at it with vec() but got nowhere.

    Could someone please point me in the right direction for extracting
    this data please?

    Thanks,

    pip

    P.S if it makes a difference I am pulling the file from an x86 based
    system and parsing it on an x86 system.
     
    pip, Aug 17, 2005
    #1
    1. Advertising

  2. pip

    Anno Siegel Guest

    pip <> wrote in comp.lang.perl.misc:
    > Hi all,
    >
    > I am trying to extract some data from a binary file using unpack. All
    > is well where I have to extract simple data types such as shorts,
    > strings and ints but then I reach this in the docs I am using:
    >
    > Here is the way a date is represented:
    >
    > MSB LSB
    > __________________________________________________________
    > | year | Month | Day | Time |
    > | 12 bits | 4 bits | 5 bits | 11 bits |
    > |__________|____________|___________|____________________|
    >
    > I have played with the hex string operator in unpack with little
    > joy. I also had a go at it with vec() but got nowhere.
    >
    > Could someone please point me in the right direction for extracting
    > this data please?


    You want the usual shifting and masking of integers, it seems. Get
    the four bytes into an integer $x (unpack), and apply

    my( $year, $month, $day, $time) = bit_split( $x, 12, 4, 5, 11);

    with

    sub bit_split {
    my $bits = shift;
    my @parts;
    for ( reverse @_ ) {
    my $mask = ( 1 << $_) - 1;
    unshift @parts, $mask & $bits;
    $bits >>= $_;
    }
    @parts;
    }

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Aug 17, 2005
    #2
    1. Advertising

  3. pip

    pip Guest

    Anno,

    > pip <> wrote in comp.lang.perl.misc:
    >> Hi all,
    >>
    >> I am trying to extract some data from a binary file using unpack. All
    >> is well where I have to extract simple data types such as shorts,
    >> strings and ints but then I reach this in the docs I am using:
    >> [ ... snip ... ]

    >
    > my( $year, $month, $day, $time) = bit_split( $x, 12, 4, 5, 11);
    >
    > with
    >
    > sub bit_split {
    > my $bits = shift;
    > my @parts;
    > for ( reverse @_ ) {
    > my $mask = ( 1 << $_) - 1;
    > unshift @parts, $mask & $bits;
    > $bits >>= $_;
    > }
    > @parts;
    > }


    This worked a charm, I would never have worked this out without your
    help. Now to go a read up on this bit shifting malarkey.

    Cheers,

    pip
     
    pip, Aug 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,686
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    845
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    455
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. sarahh

    sum of 64 bits using 32 bits cpu

    sarahh, May 13, 2008, in forum: C Programming
    Replies:
    18
    Views:
    545
    Richard Tobin
    May 14, 2008
  5. Aaron Brice

    pack/unpack bits

    Aaron Brice, Aug 6, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    139
    Kien Ha
    Aug 6, 2003
Loading...

Share This Page