Bogdan
Bogdan

Reputation: 349

Python and ebcdic decoding (zos 1047)

I have a function that should receive data in hex EBCDIC format and convert it to ASCII.

For example, transforming the data, F1F1F0F0 should give me a 1100 in ASCII, or 31313030 in hex ASCII.

What I've found is this:

    def __decode_ASC_EBCDIC_DT(self, data):
    if (data[0] == '3'):
        #HEX ASCII
        dt_ = ''.join(chr(int(data[i:i + 2], 16)) for i in range(0, len(data), 2))
        return dt_
    elif (data[0] == 'F'):
        #HEX EBCDIC
        try:
            tmp  = bytearray(ord(c) for c in data)
            dt_ = ''.join(tmp.decode('cp500'))
        except:
            print('can\'t convert:' + data)
        return dt_

but it seems that CP500 is transfroming my data in 'ãããã' and in this case this is incorrect. (tmp is correct bytearray(b'F1F1F0F0'))

Any ideas, or should I make my own dictionary for EBCDIC?

Upvotes: 0

Views: 2768

Answers (2)

Bogdan
Bogdan

Reputation: 349

The error in the above code was that the input should be treated as HEX (Thanks to Kevin for pointing it out).

The corrected code:

    def __decode_ASC_EBCDIC_DT(self, data):
if (data[0] == '3'):
    #HEX ASCII
    dt_ = ''.join(chr(int(data[i:i + 2], 16)) for i in range(0, len(data), 2))
    return dt_
elif (data[0] == 'F'):
    #HEX EBCDIC
    try:
        dt_ = ''.join(bytearray.fromhex(data).decode('cp500'))
    except:
        print('can\'t convert:' + data)
    return dt_

Upvotes: 0

Kevin McKenzie
Kevin McKenzie

Reputation: 654

bytearray(b'F1F1F0F0') is not what you seem to think it is. It's the byte representation of the ASCII string 'F1F1F0F0'.

>>input = bytearray(b'F1F1F0F0')
>>> for item in input:  print(item)
70
49
70
49
70
48
70
48

What you're passing in is, from a EBCDIC point of view, meaningless: EBCDIC 48, 49, and 70 are undefined, so codecs.decode is going to give meaningless output.

I'm not sure where you're getting the input from, but if you want to convert an EBCDIC string to ascii, you can do this:

>>> input=bytearray([241, 241, 240, 240])
>>> for item in input: print(item)
241
241
240
240
>>> import codecs
>>> codecs.decode(input, 'cp500')
'1100'

Upvotes: 1

Related Questions