Python:将字符串转换为其二进制表示形式

时间:2014-01-16 07:36:21

标签: python string python-2.7 binary-data shellcode

我使用Python 2.7.X.
我有文本文件,其中包含以下内容:

\xe87\x00\x10LOL  

请注意,这是文本本身,而不是它的二进制表示(意思是第一个字符是'\\',而不是0xe8) 当我读它(作为二进制)时,我得到:

a = "\\\\xe87\\\\x00\\\\x10LOL"  

因为它是一个文本文件。

我想将其转换为二进制形式,这意味着我想获得一个以字符
开头的文件 0xe8,0x37,0x00,0x10,0x4c,0x4f,0x4c。
(注意0x4c =='L',0x4f =='O')。

我该怎么做?
尝试了各种各样的解决方案,如hexlify \ unhexlify,int(c,16),但似乎我遗漏了一些东西。
另请注意,文件的长度各不相同,因此不太喜欢struct.pack。

3 个答案:

答案 0 :(得分:2)

使用string-escape or unicode-escape encoding

>>> content = r'\xe87\x00\x10LOL'
>>> print content
\xe87\x00\x10LOL
>>> content
'\\xe87\\x00\\x10LOL'
>>> content.decode('string-escape')
'\xe87\x00\x10LOL'
>>> map(hex, map(ord, content.decode('string-escape')))
['0xe8', '0x37', '0x0', '0x10', '0x4c', '0x4f', '0x4c']

>>> bytes(map(ord, content.decode('string-escape')))
'[232, 55, 0, 16, 76, 79, 76]'

>>> bytearray(map(ord, content.decode('string-escape')))
bytearray(b'\xe87\x00\x10LOL')

答案 1 :(得分:2)

这是一种方法:

In [26]: a = r"\xe87\x00\x10LOL"

In [27]: b = ast.literal_eval("'" + a + "'")

In [28]: open("test.dat", "w").write(b)

In [29]: 
[1]+  Stopped                 ipython
$ xxd test.dat
0000000: e837 0010 4c4f 4c                        .7..LOL

(可能有比literal_eval更好的工具,但这是早上这个早上的第一个想到的。)

答案 2 :(得分:0)

"".join([chr(int(i,16)) for i in data.split("\\x") if i])

相关问题