【问题标题】:HexString to packed EBCDIC stringHexString 到打包的 EBCDIC 字符串
【发布时间】:2019-10-13 13:49:52
【问题描述】:

我需要将 '767f440128e1a00a' 十六进制数据转换为打包的 EBCDIC 字符串。我希望将所有result 结果合并为一个字符串,但 python 给出 Unicode 错误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)

【问题讨论】:

    标签: python encoding codec


    【解决方案1】:

    这是基于Python byte representation of a hex string that is EBCDIC的代码,其中提到“根据this,您需要使用'cp500'进行解码”

    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)
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-12
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2016-10-29
      • 1970-01-01
      相关资源
      最近更新 更多