Python的创建从十六进制字节数组 [英] Python creating an array from hex bytes

查看:2802
本文介绍了Python的创建从十六进制字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

 0C A8 F0 D6 02 00 00 00 00 D0 D1 1C 10 D2 D3 00 00 01 D7 D4 78 20 FF

正如你所看到的,它包含十六进制值,我想将其改造成一个字节数组,使用Python的 2.4.4 (不3.x中,所以我没有用字节组)。实现它按我所知的唯一方式是这样的:

  I = []
i.append(的0x0C)
i.append(0xa8)
i.append(0XF0)#...等
.....
Z =''。加入(CHR(三)在I C)

但是,这是可怕的。任何好的提示如何有效地解决这个?


解决方案

 '0C A8 F0 D6 02 00 00 00 00 D0 1C D1 10 D2 00 D3 00 D7 01 D4 78 20 FF .replace('','')由Matchi.com提供回到code(十六进制)

I have the following string:

"0c a8 f0 d6 02 00 00 00 00 d0 1c d1 10 d2 00 d3 00 d7 01 d4 78 20 ff"

As you can see, it contains hex values and I want to transform it into an array of bytes, using Python 2.4.4 (NOT 3.x, so I don't have the useful bytearray). The only way to achieve it as per my knowledge is something like:

i = []
i.append(0x0c)
i.append(0xa8)
i.append(0xf0) # ... and so on
.....
z = ''.join(chr(c) for c in i)

But this is horrible. Any good hint how to solve this efficiently?

解决方案

'0c a8 f0 d6 02 00 00 00 00 d0 1c d1 10 d2 00 d3 00 d7 01 d4 78 20 ff'.replace(' ', '').decode('hex')

这篇关于Python的创建从十六进制字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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