#! /usr/bin/perl use strict; use warnings; # convert values into bitstring (check sizes before!) my $bits = sprintf( "%05b%01b%010b%08b", 2, 0, 64, 2 ); print $bits, "\n"; # split bitstring into bitstring per byte (length of $bits MUST be a multiple of 8) my @bytes = $bits =~ m/([01]{8})/g; print "String: $_\n" for @bytes; # convert bitstrings to numbers @bytes = map { oct "0b$_" } @bytes; print "Number: $_\n" for @bytes;