#!/usr/bin/perl use warnings; use strict; my @bits = ( 1, 0, 0, 0, 0, 0, 1, 0, # A / 0x41 / 0b01000001 0, 1, 0, 0, 0, 0, 1, 0, # B / 0x42 / 0b01000010 1, 1, 0, 0, 1, 0, 1 # incomplete byte ); my $string = join q(), @bits; my $incomplete = @bits % 8 || -@bits; # The || part handles complete bytes. print pack "b*", substr $string, 0, -$incomplete; splice @bits, 0, -$incomplete; # Remove the processed part. print "\n@bits\n";