in reply to Re: How to unpack numbers with varying sizes?
in thread How to unpack numbers with varying sizes?

Because the data includes numbers encoded in 3, 5, 6, and 7 bytes as well.
  • Comment on Re^2: How to unpack numbers with varying sizes?

Replies are listed 'Best First'.
Re^3: How to unpack numbers with varying sizes?
by ikegami (Patriarch) on Feb 13, 2011 at 06:16 UTC

    unpack doesn't support those sizes directly. You could extract them in parts, then assemble them.

    # 3-bytes, network order my ($o1, $o2, $o3) = unpack('CCC', $_); my $n = ($o1 << 16) | ($o2 << 8) | $o3; -or- my ($n1, $n2) = unpack('Cn', $_); my $n = ($n1 << 16) | $n2;

    Alternatively, you could add padding.

    # 3-bytes, network order my $n = unpack('N', "\x00".$_);
Re^3: How to unpack numbers with varying sizes?
by Anonymous Monk on Feb 13, 2011 at 06:06 UTC
    Because the data includes numbers encoded in 3, 5, 6, and 7 bytes as well.

    And why do you think that is a problem? a 3 byte integer fits into a 4 byte, or 8 byte integer without problems