转换字符串:十六进制>十进制&解析

时间:2015-10-20 19:21:58

标签: python string hex

任何人都可以帮我解决将整个字符串从十六进制转换为十进制的问题吗?

字符串如下所示: 0E:E9:6E:00:31:2E:36:2E:38:4D:61:6A:1207:72:00:00:00:00:00:00:00:00:00:00:00: 00:00:00:00:00:00:00:00:0B:32:31:45:39:33:35:42:41:44:37:30:00

我一直在使用这个网站,但是一直一个接一个地执行这些任务非常繁琐:http://www.binaryhexconverter.com/hex-to-decimal-converter

我需要它用于python,我用bytearray数据包发送编码一些东西,它看起来像这样:         my_bytes = bytearray([14,233,110,0,49,46,54,46,56,77,97,106,111,114,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ,0,0,0,0,0,11,50,49,69,57,51,53,66,65,68,55,48,0])

因此以某种方式解析该格式是理想的。感谢您的关注! :)

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情:

input = '0e:e9:6e:00:31:2e:36:2e:38:4d:61:6a:6f:72:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:0b:32:31:45:39:33:35:42:41:44:37:30:00'

parsed = input.split(':')

outstring = ','.join(str(int(val, 16)) for val in parsed)

my_bytes = bytearray(outstring)

此时你可以打印my_bytes

print my_bytes

14,233,110,0,49,46,54,46,56,77,97,106,111,114,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,50,49,69,57,51,53,66,65,68,55,48,0

或将其保存到文件等