[Tutor] Converting EBCDIC characters to ASCII

bob gailer bgailer at gmail.com
Thu Nov 13 18:58:48 CET 2008


Steve Bricker wrote:
> I am trying to build an application for work that FTPs a file from an 
> IBM mainframe to my Windows desktop then process it.  The 
> EBCDIC-to-ASCII conversion works nicely (using a BAT file I created) 
> except for three packed-decimal data fields (for example, 
> X'0000000000000000014C' is equal to integer 14).  I am assuming I need 
> to go through this byte-by-byte to convert.  Is that the best 
> approach, or is there a better way?

What does a BAT file have to do with Python?

Yes you will have to process the packed-decimal data fields byte-by 
byte. If the data volume is high then I'd construct a dictionary with a 
key for each possible byte value, lookup each byte in the dictionary and 
accumulate the total:

d={}
for l in range(10):
    for r in range(10):
        k = l*16+r
        v = l*10+r
        d[chr(k)] = v

This gives a lookup table for all but the rightmost byte. If a byte 
contains for example '\x91' then d[byte] gives 91

The rightmost byte holds 1 digit and the sign (C=plus) in your example.  
Extend the dictionary to handle these:

for l in range(10):
    for r in (11, 13):
        k = l*16+r
        v = l*10
        d[chr(k)] = (v, 1)
for l in range(10):
    for r in (11, 13):
        k = l*16+r
        v = l*10
        d[chr(k)] = (v, -1)

m = 10
value, sign = d[bcdstring[-1]]
for byte in bcdstring[-2::-1]:
    value += m*d[byte]
    m *= 100
value = value * sign

-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?



More information about the Tutor mailing list