Article 13227 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:13227
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!wupost!udel!MathWorks.Com!news.kei.com!ub!netfs.dnd.ca!manitou.cse.dnd.ca!phousto
From: phousto@cse.dnd.ca (Phil Houstoun)
Subject: Re: EBCDIC -> ASCII anyone got a prog?
Message-ID: <1994Apr28.020611.15389@cse.dnd.ca>
Organization: Canadian System Security Centre
References: <CovJFA.B4L@newcastle.ac.uk> <2plg5q$df@hibbert.meiko.com>
Date: Thu, 28 Apr 94 02:06:11 GMT
Lines: 146

Following script implements a table-driven character conversion process. The
two attached tables provide ASCII-EBCDIC and EBCDIC-ASCII conversions, albeit
imperfectly.  Forgive any non-intuitive constructs, I've only got one toe in
the water so far. Cheers.

#!/usr/local/bin/perl
###############################################################################
#
# NAME
#    ttr.pl
#
# SYNOPSIS
#    ttr -t table < stdin > stdout
#
# DESCRIPTION
#    Perl script for performing table-driven character conversions.
#
# AUTHOR
#	phousto@cse.dnd.ca
#
# DATE
#    940327
#
##############################################################################
$THISPROG = 'ttr';
$VERSION = '1.0';
$IDENT = sprintf("%s(%s)", $THISPROG, $VERSION);
$USAGE = sprintf("Usage: %s -t table < stdin > stdout", $THISPROG);
$BUFSIZ = 1024;

# Parse cmd line

while($ARGV[0] =~ /^-/) {
	$_ = shift;
	if(/^-[tT]/) {
		$TBLNAME = $ARGV[0];
	}
}

# Open conversion table file

defined $TBLNAME ||
	die $IDENT, ": ERROR - table file name not provided\n$USAGE\n";
open(TABLE, $TBLNAME) ||
	die "$IDENT: ERROR - Can't open table file '$TBLNAME' ($!)\n";

# Slurp up conversion table

while(<TABLE>) {
	if(/^[^#\/]/) {
		chop;
		push(@tbl, split(/\s*\D+\s*/));
	}
}
close(TABLE);

# Filter STDIN to STDOUT

while(read(STDIN, $buf, $BUFSIZ) > 0) {
	for($i = 0; $i < length($buf); $i++) {
		vec($buf, $i, 8) = $tbl[vec($buf, $i, 8)];
	}
	print $buf;
}

# END OF SCRIPT

###############################################################################
# 
# NAME
#    a2e.tbl
#
# DESCRIPTION
#
#   Table for converting ASCII to IBM standard EBCDIC to be used with ttr.pl.
#	Numeric value of input character used as offset into this table to
#	provide output character.
#
# NOTES
#
#    Comments are lines starting with '#', or '/'.
#    All values are decimal (leading zeroes are just for looks).
#    Values are delimited by one to n non-digit characters optionally 
#    surrounded by whitespace.
#
###############################################################################

000, 001, 002, 003, 055, 045, 046, 047, 022, 005, 037, 011, 012, 013, 014, 015,
016, 017, 018, 060, 061, 050, 038, 023, 024, 025, 063, 039, 028, 029, 030, 031,
064, 079, 127, 123, 091, 108, 080, 125, 077, 093, 092, 078, 107, 096, 075, 097,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 122, 094, 076, 126, 110, 111,
124, 193, 194, 195, 196, 197, 198, 199, 200, 201, 209, 210, 211, 212, 213, 214,
215, 216, 217, 226, 227, 228, 229, 230, 231, 232, 233, 074, 224, 090, 095, 109,
121, 129, 130, 131, 132, 133, 134, 135, 136, 137, 145, 146, 147, 148, 149, 150,
151, 152, 153, 162, 163, 164, 165, 166, 167, 168, 169, 192, 106, 208, 161, 007,
#
# ASCII extended chars. not defined in EBCDIC
#
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000


###############################################################################
# 
# NAME
#    a2e.tbl
#
# DESCRIPTION
#
#   Table for converting IBM standard EBCDIC to ASCII for use with ttr.pl.
#	Numeric value of input character used as offset into this table to
#	provide output character.
#
# NOTES
#
#    Comments are lines starting with '#', or '/'.
#    All values are decimal (leading zeroes are just for looks).
#    Values are delimited by one to n non-digit characters optionally 
#    surrounded by whitespace.
#
###############################################################################

000, 001, 002, 003, 000, 009, 000, 127, 000, 000, 000, 011, 012, 013, 014, 015,
016, 017, 018, 000, 000, 000, 008, 023, 024, 025, 000, 000, 028, 029, 030, 031,
000, 000, 000, 000, 000, 010, 022, 027, 000, 000, 000, 000, 000, 005, 006, 007,
000, 000, 021, 000, 000, 000, 000, 004, 000, 000, 000, 000, 019, 020, 000, 026,
032, 000, 000, 000, 000, 000, 000, 000, 000, 000, 091, 046, 060, 040, 043, 033,
038, 000, 000, 000, 000, 000, 000, 000, 000, 000, 093, 036, 042, 041, 059, 094,
045, 047, 000, 000, 000, 000, 000, 000, 000, 000, 124, 044, 037, 095, 062, 063,
000, 000, 000, 000, 000, 000, 000, 000, 000, 096, 058, 035, 064, 039, 061, 034,
000, 097, 098, 099, 100, 101, 102, 103, 104, 105, 000, 000, 000, 000, 000, 000,
000, 106, 107, 108, 109, 110, 111, 112, 113, 114, 000, 000, 000, 000, 000, 000,
000, 126, 115, 116, 117, 118, 119, 120, 121, 122, 000, 000, 000, 000, 000, 000,
000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000,
123, 065, 066, 067, 068, 069, 070, 071, 072, 073, 000, 000, 000, 000, 000, 000,
125, 074, 075, 076, 077, 078, 079, 080, 081, 082, 000, 000, 000, 000, 000, 000,
092, 000, 083, 084, 085, 086, 087, 088, 089, 090, 000, 000, 000, 000, 000, 000,
048, 049, 050, 051, 052, 053, 054, 055, 056, 057, 000, 000, 000, 000, 000, 000

.