#!/usr/bin/perl

# So: looking at pack and unpack.
#

my $string = "abcd";

print "The string is '$string' ",
      "which has ", length($string), " bytes ",
      "or ", 8*length($string), " bits.\n";

print "As base 10 numbers these bytes are ( ";
print ord(substr($string,$_,1)) . " " for 0..length($string)-1;
print ").\n";

print "As hex numbers they are ( ";
printf("%x "x length($string), map {ord($_)} split(//,$string));
print ").\n";

print "Each 4 bit chunk as a hex number is '", unpack("H*", $string), "'.\n";

my $ones_and_zeros = unpack("B*",$string);
my @nibbles; 
push @nibbles, substr($ones_and_zeros,4*$_,4) for 0..length($ones_and_zeros)/4;
print "In ones and zeros '$ones_and_zeros'\n";
print "or ( @nibbles ).\n";

my $string_again = pack("B*", $ones_and_zeros);
print "Converting the 1's and 0's back to a string gives '$string_again'.\n";






