in reply to How to unpack numbers with varying sizes?

and could only find a solution for integers of size 1, 2, 4, and 8 bytes

Why is that a problem? Maybe youwant Math::BigInt?

  • Comment on Re: How to unpack numbers with varying sizes?

Replies are listed 'Best First'.
Re^2: How to unpack numbers with varying sizes?
by Anonymous Monk on Feb 13, 2011 at 02:49 UTC
    Because the data includes numbers encoded in 3, 5, 6, and 7 bytes as well.

      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".$_);
      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