Summary: How to convert ASCII to EBCDIC

From: Randy J. Parker (randy@mobiledyne.com)
Date: Fri Apr 16 1999 - 10:17:39 CDT


Question:
How do I convert data convert data back and forth between ASCII and EBCDIC?

Four Answers and a follow-up on "iconv(3)":

42 replies point out that "dd" includes a conversion for files. Since my
data is in a C program, shelling out and running an external program would
be grossly inefficient. On the other hand, if your data are in a file, the
conversion is a snap:
   # ASCII to EBCDIC example:
   % dd if=file.ascii of=file.ebcdic conv=ebcdic

2 replies (Johnie Stafford & Daniel Ellis) refer me to a very handy
program called "recode" from the FSF:
   ftp://ftp.gnu.org/pub/gnu/recode
"recode" handles conversions between 150 different characters sets.
Naturally, the arrays I was after are in this program (as decimal, though I
prefer hex for character sets).
"recode" was written by Francois Pinard in 1994. Thank you Francois!

3 replies actually sent me the conversion arrays (George White, Erin Fritz,
Tom Anderson). These are exactly what I need!

Roger Fuji enclosed the following shell script and C code, which allows you
to conveniently re-generate the arrays. This is a very handy thing to
remember, and I enclose it here for "sun-managers" posterity:

   # Make an ASCII to EBCDIC array:
   OUTFILE=/tmp/out

   nawk 'BEGIN { for(i=0 ; i<256; ++i) printf("%c",i); }' < /dev/null | dd
conv=ebcdic > $OUTFILE
   cat >e.c <<EOF

   # Print the array from a C program:
   #include <stdio.h>
   int main(int argc, char *argv[])
   {
       int i;
       int c;
       printf("unsigned char a2e[] = {\n");
       for(i=0; i<255; ++i) {
           c= getchar();
           printf("\t%02x,\n", c);
           }
       c= getchar();
       printf("\t%02x\n};\n", c);
    }

    # Compile and run the program:
    gcc -o a2e e.c
    ./a2e < $OUTFILE

And finally, no replies even mentioned "iconv()". I'm disappointed, as I
think it provides the perfect C API for such a chore. Too bad it doesn't
support EBCDIC.

Thanks to the entire sun-managers community!
Every single reply pointed me in a valid direction; the bozo count was zero!

Randy J. Parker
MobileDyne Systems, Inc.
Atlanta, GA
http://www.mobiledyne.com



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:13:18 CDT