#!/usr/bin/perl -w use strict; { my( $quote7, $quote8, %quote, %unquote ); BEGIN { $quote7= pack "C", 0x7e; # Any 7-bit char. $quote8= pack "C", 0x7f; # Any _other_ 7-bit char. @quote{ $quote7, $quote8 }= ( $quote7.$quote7, $quote7.$quote8 ); @quote{ map { pack "C", $_ } 0x80..0xff }= map { $quote8 . pack "C", $_ } 0..0x7f; %unquote= reverse %quote; } sub strip8 { my( $bin )= @_; $bin =~ s#([$quote7$quote8\x80-\xff])#$quote{$1}#go; return $bin; } sub restore8 { my( $str )= @_; $str =~ s#([$quote7$quote8].)#$unquote{$1}#gos; return $str; } } die "Usage: $0 file\n" if 1 != @ARGV; open IN, "<$ARGV[0]" or die "Can't read $ARGV[0]: $!\n"; binmode IN; undef $/; my $in= ; my $out= strip8( $in ); my $end= restore8( $out ); die "Well, that didn't work!" if $in ne $end;