in reply to Can unpack add zero bytes before converting?

I have not been able to come up with a way to do it in a single unpack, but if I post my solution maybe someone will come up with something better:

# Arguments are datum length (5, 6, 7, or 8) and stream data (whose # length must be a multiple of the datum length) sub unpack_stream { my ( $length, $stream ) = @_; $length ||= 5; $length >= 5 and $length <= 8 or die "Length $length must be between 5 and 8 inclusive\n"; my $pad = pack 'C*', ( 0 ) x ( 8 - $length ); return map { unpack 'Q<', "$_$pad" } unpack "(a$length)*", $stream +; }

Replies are listed 'Best First'.
Re^2: Can unpack add zero bytes before converting?
by tybalt89 (Monsignor) on Sep 12, 2021 at 19:00 UTC

    Maybe unpack can't but pack can :)

    try:

    return unpack '(Q<)*', pack '(a8)*', unpack "(a$length)*", $stream;

    And you no longer need $pad or the map.

    semi-tested :)

      See? I knew someone would come up with something cleaner. Thanks.

      I understand the OP to have requested that it be done in a single unpack, but I'm convinced that is not possible -- a challenge for someone to come along and prove me wrong. And that is how knowledge grows.