Encodes every byte from standard input as four OCR-safe characters, wrapping at 32 encoded characters per line. Mapping: 00=A, 01=M, 10=3, 11=7.
#!/usr/bin/env perl
'ocr4_encode.pl';
=head1 NAME
ocr4_encode.pl - encode binary input using four OCR-safe characters
=head1 SYNOPSIS
producer | perl ocr4_encode.pl
=head1 DESCRIPTION
Reads a binary stream from standard input. Each byte is written as four
characters using C<00=A>, C<01=M>, C<10=3>, and C<11=7>.
=cut
use strict;
use warnings;
binmode STDIN;
binmode STDOUT;
my @symbol = qw(A M 3 7);
my $buffer;
my $column = 0;
while (read(STDIN, $buffer, 64 * 1024)) {
for my $byte (unpack 'C*', $buffer) {
print $symbol[($byte >> 6) & 3],
$symbol[($byte >> 4) & 3],
$symbol[($byte >> 2) & 3],
$symbol[$byte & 3];
$column += 4;
if ($column == 32) {
print "\n";
$column = 0;
}
}
}
die "Error reading standard input: $!\n" if $!;
print "\n" if $column;
some_binary_command | perl ocr4_encode.pl