python - HexString 到打包的 EBCDIC 字符串

标签 python encoding codec

我需要将“767f440128e1a00a”十六进制数据转换为打包的 EBCDIC 字符串。我希望所有 result 结果都变成一个字符串,但 python 给出 Unicode 错误 UnicodeDecodeError: 'utf-8' codec can't Decode byte 0xe1 inposition 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 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)

输出:

enter image description here

关于python - HexString 到打包的 EBCDIC 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364186/

相关文章:

python - 将 Python 列表拆分为匹配列表项定界符上的列表列表的正确方法

python - 不断增长的 matplotlib 条形图

python - Django 从apache 获取环境变量

R 语料库弄乱了我的 UTF-8 编码文本

java - 在Java中打印字符串编码有线unicode

android - Android app如何知道底层硬件支持某种硬件解码加速?

c++ - OpenCV/FFMpeg图像捕捉问题

python - 匹配 pandas 列中列表和列表之间的相似元素

c++ - 使用 OpenAL 的硬件加速音频解码

c# - 使用 HttpResponse 在 C# 中下载具有非英文字符的文件