pack/unpack bits

Discussion in 'Perl Misc' started by Aaron Brice, Aug 6, 2003.

  1. Aaron Brice

    Aaron Brice Guest

    I have a problem with using unpack to separate out some arbitrary bits
    from some binary data received using some wacky protocol. The first bit
    of the binary message is the "rbit", the next 15 bits is the device id,
    etc. What I'd like to use is unpack() to map the bits to various
    variables such as:

    #!/usr/bin/perl

    $msg = pack ("H*", "8000ffff");

    ($rbit, $dev, $buffer) = unpack("B1B15B*", $msg);

    print "rbit: $rbit\n";
    print "devid: $devid\n";
    print "buffer: $buffer\n";

    I would expect this to have rbit set to 1, devid set to 000000000000000,
    and buffer set to 1111111111111111. Instead, the result is:

    rbit: 1
    devid: 000000001111111
    buffer: 11111111

    It seems like the "B1" token in the unpack template reads a whole byte and
    gives the first bit of it, the B15 token reads the next two bytes and
    gives the first 15 bits of them, and B* gets the rest, which is only one
    byte left. There's a lot of missing bits there! Is there a way for me to
    use unpack() to do what I want without having to use a more awkward
    masking and shifting sytem?

    Thanks,
    Aaron
     
    Aaron Brice, Aug 6, 2003
    #1
    1. Advertising

  2. Aaron Brice

    Kien Ha Guest

    Aaron Brice wrote:
    > I have a problem with using unpack to separate out some arbitrary bits
    > from some binary data received using some wacky protocol. The first bit
    > of the binary message is the "rbit", the next 15 bits is the device id,
    > etc. What I'd like to use is unpack() to map the bits to various
    > variables such as:
    >
    > #!/usr/bin/perl
    >
    > $msg = pack ("H*", "8000ffff");
    >
    > ($rbit, $dev, $buffer) = unpack("B1B15B*", $msg);
    >
    > print "rbit: $rbit\n";
    > print "devid: $devid\n";
    > print "buffer: $buffer\n";
    >
    > I would expect this to have rbit set to 1, devid set to 000000000000000,
    > and buffer set to 1111111111111111. Instead, the result is:
    >


    ($rbit, $dev, $buffer) = unpack "A1A15A*", unpack "B*", $msg;


    --
    Kien
     
    Kien Ha, Aug 6, 2003
    #2
    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. Stacy Mader
    Replies:
    4
    Views:
    855
    Ekkehard Morgenstern
    Nov 22, 2003
  2. tmp123
    Replies:
    5
    Views:
    903
    Tim Roberts
    May 4, 2007
  3. joe shoemaker

    Python pack and unpack question

    joe shoemaker, Jul 15, 2008, in forum: Python
    Replies:
    0
    Views:
    338
    joe shoemaker
    Jul 15, 2008
  4. Kero
    Replies:
    0
    Views:
    225
  5. Bill Atkins
    Replies:
    6
    Views:
    150
    Daniel Berger
    Jun 14, 2004
Loading...

Share This Page