Article 12216 of comp.lang.perl:
Path: feenix.metronet.com!news.utdallas.edu!wupost!howland.reston.ans.net!news.moneng.mei.com!uwm.edu!daffy!uwvax!sinetnews!news.u-tokyo.ac.jp!wnoc-tyo-news!scslwide!wsgw!headgw!cvgw3!tshiono
From: tshiono@cv.sony.co.jp (Toru Shiono)
Newsgroups: comp.lang.perl
Subject: Re: EBCDIC -> ASCII problems
Message-ID: <TSHIONO.94Apr02074955@aquarius.cv.sony.co.jp>
Date: 2 Apr 94 07:49:55 GMT
References: <2nfb3a$3io@meaddata.meaddata.com>
Sender: news@cv.sony.co.jp (Usenet News System)
Organization: Sony Corporation, Tokyo, Japan
Lines: 56
Nntp-Posting-Host: aquarius
X-Newsreader: prn Ver 1.10
In-reply-to: petew@meaddata.com's message of 31 Mar 94 20:16:42 GMT

In article <2nfb3a$3io@meaddata.meaddata.com>
	petew@meaddata.com (Pete Williams) writes:

:I'm trying to write a perl library routine that converts from EBCDIC to
:ASCII format.  I would like to do this in the most efficient way possible.
:
:First of all, has anyone done this (without opening a filehandle to pipe
:the input through "dd")? 
:
:I used the conversion tables from the GNU version of "dd" which specifies
:the octal representations of the ASCII & EBCDIC characters to build an
:associate array mapping of these octal values.  However, the translation 
:doesn't take place properly.  I seem to be getting lost somewhere in the 
:character <-> decimal <-> octal <-> hex translations.
:
:Right now, the procedure is something like this:
:
:	1. Get an ebcdic character, "$ebcdic_char"
:	2. $ebcdic_ord = ord($ebcdic_char);
:	3. $ebcdic_oct = sprintf("%o",$ebcdic_ord);
:	3. $ascii_oct = $ae_tbl{$ebcdic_oct};
:	4. $ascii_ord = oct($ascii_oct);
:	5. $ascii_char = sprintf("%c",$ascii_ord);
:
It will be better to map character to character directly rather than
translating it to octal representation:

# make a table
#
# This is just an example to show how data are stored.
# You can write it without using `dd' if you will.

if ($pid = open(PIPE, '-|')) {
        $count = 0;
        while (read(PIPE, $ebc, 1)) {
                $e2a{$ebc} = pack('C', $count++);
        }
} elsif (defined $pid) {
        open(STDOUT, '| dd conv=ebcdic');
        for (0 .. 255) {
                print pack('C', $_);
        }
        exit;
} else {
        die "fork: $!";
}

Now you can say:

	1. Get an ebcdic character, "$ebcdic_char"
	2. $ascii_char = $e2a{$ebcdic_char};
or:
	1. Get an ebcdic srting "$ebcdic_string"
	2. $ascii_sting = join('', @e2a{split(//, $ebcdic_string)});
--
Toru "devil-may-care" Shiono          Sony Corporation, JAPAN