[Tutor] Question regarding character conversion usage.

Terry Carroll carroll at tjc.com
Thu Aug 19 05:19:41 CEST 2004


On Wed, 18 Aug 2004, Steve Robb wrote:

> I have a project which requires the conversion of a file from ASCII to
> EBCDIC.  My search on google produced the code by a Don Perterson, with his
> notes re: "The arrays were taken from the Snippets collection."  

You can use Python's own routines for this.  CP500 appears to be standard 
EBCDIC.

>>> import codecs
>>>
>>> ebcdic_input = '\xe2\xd7\xc1\xd4'  # "SPAM" in EBCDIC
>>>
>>> e2a = codecs.getdecoder("CP500")
>>> a2e = codecs.getencoder("CP500")
>>>
>>> (asc_out, strlen) = e2a(ebcdic_input)
>>> asc_out, strlen
(u'SPAM', 4)
>>>
>>> (ebc_out, strlen) = a2e(asc_out)
>>> ebc_out, strlen
('\xe2\xd7\xc1\xd4', 4)

It was nice to see a little EBCDIC again.  I'm a former architect for 
Amdahl, which made IBM-compatible mainframes.  I grew up on EBCDIC, and 
never really have learned ASCII intuitively as I did EBCDIC.



More information about the Tutor mailing list