HexString to packed EBCDIC string

泄露秘密 提交于 2020-01-25 00:26:52

问题


I need to convert '767f440128e1a00a' hex data to packed EBCDIC string. I want all result outcomes into one string but python is giving Unicode error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 0: unexpected end of data

s='767f440128e1a00a'
output = []
DDF = [1]
distance = 0
for y in range (1,len(s[2:])):
    for x in DDF:
        if s[2:][distance:x*2+distance]!='':
            output.append(s[2:][distance:x*2+distance]) 
        else:
            continue
        distance += x*2
print(output)
final=[]
result=''
bytearrya=[]
for x in output:
    result=(str(bytearray.fromhex(x).decode()))
    x = codecs.decode(x, "hex")
    final.append(x)

回答1:


Here is the code based on Python byte representation of a hex string that is EBCDIC that mentioned "According to this, you need to use 'cp500' for decoding"

Codec / Aliases                            / Languages
cp500 / EBCDIC-CP-BE, EBCDIC-CP-CH, IBM500 / Western Europe
my_string_in_hex = '767f440128e1a00a'
my_bytes = bytearray.fromhex(my_string_in_hex)
print(my_bytes)

my_string = my_bytes.decode('cp500')
print(my_string)

Output:



来源:https://stackoverflow.com/questions/58364186/hexstring-to-packed-ebcdic-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!