sub dec2bin { my $str = unpack("B32", pack("N", shift)); # $str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros return substr( $str, -6 ); }