HexString到打包的EBCDIC字符串

问题描述 投票:1回答:1

我需要将'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个回答
0
投票

[当您想解码此部分时会发生这种情况:\xe1但在UTF-8中该字节不应与之相伴,应该跟随两个字节检查此表:

UTF-8 encoding table and Unicode characters

是说unexpected end of data,因为该字节后面应有上表中的两个字节。我不确定您要在这里做什么,但这就是为什么您遇到此错误的原因。

您可以像这样稍微更改代码,以查看所使用的字节序列:

# after printing the output try this:
print(output)
# convert output list to bytearray to decode it later
utf8_literal = bytearray()
for x in output:
    utf8_literal += bytearray.fromhex(x)

print(utf8_literal)  # bytearray(b'\x7fD\x01(\xe1\xa0\n')  

# try to decode it to utf-8
codecs.decode(utf8_literal)  # 'utf-8' codec can't decode bytes in position 4-5: invalid continuation byte

消息有些不同,但仍然说相同的事情,即字节序列不正确,我不知道您是要测试随机数还是要从其他地方获取它们,但是您无法将其转换为[C0 ]如果不是有效的utf-8字节序列。

© www.soinside.com 2019 - 2024. All rights reserved.