fork download
  1. #!/usr/bin/perl
  2.  
  3. # Idiom #275 Binary digits to byte array
  4.  
  5. use v5.18;
  6. use strict;
  7.  
  8. my $s = '1000' . '0010' . '0101' . '1010'; # AZ
  9.  
  10. my @a;
  11. for ( my $i = 0; $i < length $s; $i += 8) {
  12. my @b = pack 'b8', substr($s, $i, 8);
  13. push @a, @b;
  14. }
  15.  
  16. say '[ "', join('" , "', @a), '" ]';
  17.  
  18. printf "ordinals of A and Z as binary = %08B%08B\n",ord('A'), ord('Z');
  19. say 'unpack with B* (descending) = ', unpack 'B*', 'AZ';
  20.  
Success #stdin #stdout 0.01s 5552KB
stdin
Standard input is empty
stdout
[ "A" , "Z" ]
ordinals of A and Z as binary = 0100000101011010
unpack with B* (descending)   = 0100000101011010