#!/usr/bin/perl use strict; use warnings; my $BUFSIZE = 8; my $data; # read the data file open(FH,"chargen") or die; binmode(FH,":raw"); read(FH,$data,$BUFSIZE); # $data contains 'ABCDEFGH' close(FH); print "Source:\n"; foreach (split //, $data) { to_binary($_); } $data = transpose($data); print "Destination:\n"; # --> cries foul in the sub for the next line foreach (split //, $data) { to_binary($_); } sub transpose { my $buf = shift; return if (length($buf) < $BUFSIZE); ($buf) = substr($buf,0,$BUFSIZE) if (length($buf) > $BUFSIZE); my @in = unpack("C*",$buf); # get only ascii vals my @out = (0,0,0,0,0,0,0,0); my $newrow = 0; for my $row (reverse 0..($BUFSIZE-1)) { for my $col (0..($BUFSIZE-1)) { $out[$newrow] |= (1 << $col) if ($in[7 - $col] >> $row & 1); } $newrow++; } return pack("C*",@out); } sub to_binary { my $byte = shift; ($byte) = split //, $byte if (length($byte) > 1); for (reverse 0..($BUFSIZE-1)) { my $b = ord($byte); print $b >> $_ & 1 ? "1" : "0"; } print "\n"; }