#!/usr/bin/env perl use warnings; use strict; sub conv_stream { my ($stream_ref) = @_; my $first = shift @$stream_ref; my $count = ( vec $first, 3, 2 ) || 1; # edit: I was using unpack and oct above # edit: No need to overwrite the "count" if first bits are 00 or 01 if ( $count > 1 ) { ( vec $first, 3, 2 ) = 0; } my $next = join q{}, splice @$stream_ref, 0, ($count-1); my $pad = (chr 0) x (4-$count); return unpack 'N', $pad . $first . $next; } # I'm not quite sure I have the right bit ordering... my @stream = map { pack 'B8', $_ } ( '01010101', # 85 '10101010', '10101010', # 10922 '11001100', '11001100', '11001100' ); # 838860 print conv_stream( \@stream ), "\n" while ( @stream );