HexString到打包的EBCDIC字符串 [英] HexString to packed EBCDIC string

查看:95
本文介绍了HexString到打包的EBCDIC字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 767f440128e1a00a十六进制数据转换为打包的EBCDIC字符串。我想将所有结果结果合并为一个字符串,但是python给出了Unicode错误 UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0xe1 :数据意外结束

  s ='767f440128e1a00a'
输出= []
DDF = [1]
距离= 0
对于y在范围内(1,len(s [2:])):
对于x在DDF中:
如果s [2:] [距离:x * 2 +距离]!='':
output.append(s [2:] [距离:x * 2 + distance])
其他:
继续
距离+ = x * 2
打印(输出)
final = []
结果=''
bytearrya = []
为x在输出中:
result =(str(bytearray.fromhex(x).decode()))
x = codecs.decode(x, hex)
final.append(x)


解决方案

以下是基于


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)

解决方案

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:

这篇关于HexString到打包的EBCDIC字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆