Ebcdic -> ascii

I’m trying to write a Ruby script that will translate data in EBCDIC and
packed BCD format into ASCII and then into a MySQL database. There are
some very handy pack and unpack functions for strings, but I don’t see any
translation from EBCDIC. Does anyone have a useful module for doing this?

– Matt

It’s not ruby, but translating EBCDIC to ASCII is easily done with
iconv. It’s part of the glibc distribution, so it should be available
anywhere you have glibc (i.e. linux, windows w/ cygwin, etc.)

Now, as far as using it from ruby, you could do something like this:

iconv = IO.popen(“iconv -f EBCDIC-US -t ASCII”, “r+”)
iconv.puts(“this is in ASCII”)
iconv.close_write
iconv.gets #=>“£\210\211¢@\211¢@\211\225@ÁâÃÉÉ%”

(which is in EBCDIC-US, although it might not be by the time my mail
agent translates it to UTF-8 :wink:

···

On Tuesday 04 March 2003 2:59 pm, Matt Lawrence wrote:

I’m trying to write a Ruby script that will translate data in EBCDIC
and packed BCD format into ASCII and then into a MySQL database.
There are some very handy pack and unpack functions for strings, but
I don’t see any translation from EBCDIC. Does anyone have a useful
module for doing this?


Wesley J. Landaker - wjl@icecavern.net
OpenPGP FP: C99E DF40 54F6 B625 FD48 B509 A3DE 8D79 541F F830

It’s also easily done using dd. The problem is that there are packed
decimal fields embedded in the record. Also, the tape this data is from
arrived without a data description. I was hoping to write a somewhat
generalized tool for interactively figuring out the format.

– Matt

···

On Wed, 5 Mar 2003, Wesley J Landaker wrote:

It’s not ruby, but translating EBCDIC to ASCII is easily done with
iconv. It’s part of the glibc distribution, so it should be available
anywhere you have glibc (i.e. linux, windows w/ cygwin, etc.)

Hi –

I’m trying to write a Ruby script that will translate data in EBCDIC
and packed BCD format into ASCII and then into a MySQL database.
There are some very handy pack and unpack functions for strings, but
I don’t see any translation from EBCDIC. Does anyone have a useful
module for doing this?

It’s not ruby, but translating EBCDIC to ASCII is easily done with
iconv. It’s part of the glibc distribution, so it should be available
anywhere you have glibc (i.e. linux, windows w/ cygwin, etc.)

Now, as far as using it from ruby, you could do something like this:

iconv = IO.popen(“iconv -f EBCDIC-US -t ASCII”, “r+”)
iconv.puts(“this is in ASCII”)
iconv.close_write
iconv.gets #=>“£\210\211¢@\211¢@\211\225@ÁâÃÉÉ%”

(which is in EBCDIC-US, although it might not be by the time my mail
agent translates it to UTF-8 :wink:

Also:

candle:~$ irb --simple-prompt

VERSION
=> “1.8.0”
require ‘iconv’
=> true
Iconv.iconv(“EBCDIC-US”, “ASCII”, “this is in ASCII”)
=> [“\243\210\211\242@\211\242@\211\225@\301\342\303\311\311”]

(I think this is/was new with 1.7.)

David

···

On Wed, 5 Mar 2003, Wesley J Landaker wrote:

On Tuesday 04 March 2003 2:59 pm, Matt Lawrence wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hey, very handy! =) I’ll have to use that in my new scripts now that I
know it exists.

···

On Tuesday 04 March 2003 4:32 pm, dblack@superlink.net wrote:

require ‘iconv’

=> true

Iconv.iconv(“EBCDIC-US”, “ASCII”, “this is in ASCII”)

=> [“\243\210\211\242@\211\242@\211\225@\301\342\303\311\311”]

(I think this is/was new with 1.7.)


Wesley J. Landaker - wjl@icecavern.net
OpenPGP FP: C99E DF40 54F6 B625 FD48 B509 A3DE 8D79 541F F830

Matt Lawrence wrote:

It’s also easily done using dd. The problem is that there are packed
decimal fields embedded in the record. Also, the tape this data is from
arrived without a data description. I was hoping to write a somewhat
generalized tool for interactively figuring out the format.

I don’t suppose you have the source for the COBOL program that
originally read this stuff… :slight_smile:

Here’s something that might help you build a map of which
columns contain EBCDIC and which contain packed decimal:

Read a record at a time, looking at the first column. If you see
something that’s not a valid EBCDIC representation of a printable
character, mark the first column as packed decimal, rewind the
file, and go on to the next column. If you reach the end of
the file, mark the column as containing EBCDIC, rewind, and go
on to the next column. When there are no more columns (hopefully,
these are fixed length records with only one record type), you’re
done.

If you invent an ASCII representation for the map, you can write
it to a file and edit it as needed in case the automatic generation
didn’t quite get it right.

With the record map in hand, you can use dd (or whatever) to do
the translation and then paste fields back in from the original
file. With any luck, you’ll have something reasonably accurate.

Louis