我必须生成一个从\ x00到\ xff的所有十六进制的列表,但是必须生成用户提供的那些。
示例:
generatehex.py 00,03
\ X01 \ X02 \ X04 \ X05 \ X06 \ X07 \ X08 \ X09 \ X0A \ X0B \ X0C \ X0D \ x0e \ X0F \ X10 \ X11 \ X12 \ X13 \ X14 \ X15 \ X16 \ X17 \ X18 \ X19 \ X1A \ X1B \ X1C \ X1D \ X1E \ X1F \ X20 \ X21 \ X22 \ X23 \ X24 \ X25 \ X26 \ X27 \ X28 \ X29 \ X2A \ X2B \ X2C \ X2D \ X2E \ X2F \ X30 \ X31 \的X32 \ X33 \ X34 \ X35 \ X36 \ X37 \ X38 \ X39 \ X3A \ X3B \ X3C \ X3D \ x3e \ X3F \ X40 \ X41 \ X42 \ X43 \ X44 \ X45 \ X46 \ X47 \ X48 \ X49 \ X4A \ X4B \ x4c \ x4d \ x4e \ x4f \ X50 \ X51 \ X52 \ X53 \ X54 \ X55 \ X56 \ X57 \ X58 \ X59 \ X5A \ x5b \ x5c \ X5D \ x5e \ X5F \ X60 \ X61 \ X62 \ X63 \ 64 \ X65 \ X66 \ X67 \ X68 \ X69 \ X6A \ X6B \ X6C \ X6D \ x6e \ x6f \ X70 \ X71 \ X72 \ X73 \ X74 \ X75 \ X76 \ X77 \ X78 \ X79 \ X7A \ x7b \ X7C \ x7d \ x7e \ 0x7F部分\ X80 \ X81 \ X82 \ X83 \ X84 \ X85 \ 86 \的x87 \ X88 \ X89 \ x8a \ x8b \ x8c \ x8d \ x8e \ X8F \ X90 \ X91 \ X92 \ X93 \ X94 \ X95 \ X96 \ X97 \ X98 \ X99 \ x9a \ x9b \ x9c \ x9d \ x9e \ x9f \ XA0 \ XA1 \ XA2 \ XA3 \ XA4 \ xa5 \ xa6 \ XA7 \ xa8版权所有\ xA9 \的Xaa \ XAB \ XAC \ XAD \ XAE \ XAF \ XB0 \ XB1 \ XB2 \ XB3 \ XB4 \ XB5 \ XB6 \ XB7 \ XB8 \ xb9 \ XBA \ XBB \ XBC \ XBD \ XBE \ XBF \ XC0 \ XC1 \ XC2 \ XC3 \ XC4 \ XC5 \ XC6 \ xc7 \ xc8 \ xc9 \ XCA \ XCB \ XCC \ XCD \ XCE \ XCF \ XD0 \ XD1 \ XD2 \ XD3 \ XD4 \ XD5 \ XD6 \ XD7 \ XD8 \ xd9 \ XDA \ XDB \ XDC \ XDD \ XDE \ XDF \ xe0 \ XE1 \ XE2 \ XE3 \ XE4 \ xe5 \ XE6 \ XE7 \ xe8 \ xe9 \ XEA \ XEB \ XEC \固定的\ XEE \ XEF \ XF0 \ XF1 \ XF2 \ XF3 \ XF4 \ XF5 \ XF6 \ XF7 \ XF8 \ xf9 \ XFA \ XFB \ XFC \ XFD \ XFE \ XFF
我现在已经使用了这段代码:
#!/usr/bin/python
import sys
if (sys.argv[1]):
bad_chars = sys.argv[1].split(',')
else:
print "You must provide the characters that you don't want in the output!"
hex = ["\\x" + format(x, '02x') for x in range(0,256) if (format(x, '02x') not in bad_chars)]
print ''.join(hex)
我现在想要的是每10行十六进制打印一个换行符,每行输入双引号,所以:
generatehex.py 00,03
“\ X01 \ X02 \ X04 \ X05 \ X06 \ X07 \ X08 \ X09 \ X0A \ X0B”
“\ X0C \ X0D \ x0e \ X0F \ X10 \ X11 \ X12 \ X13 \ X14 \ X15”
“\ X16 \ X17 \ X18 \ X19 \ X1A \ X1B \ X1C \ X1D \ X1E \ X1F”
等等,也许没有行之间的额外换行符。
我该怎么办?
答案 0 :(得分:0)
有一个有用的grouper
recipe in itertools
可以解决这个问题:
from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
for row in grouper(hex_, 10, ""):
print "".join(row)
请注意,您不应将列表hex
命名为built-in function。
答案 1 :(得分:0)
试试这个:
a, b = '\x04', '\xff'
print ''.join([('"' if (i - ord(a)) % 10 == 0 else '')
+ (r'\x' + format(i, r'02x'))
+ ('"\n' if (i - ord(a)) % 10 == 9 else '')
for i in range(ord(a), ord(b))]) \
+ '"' if not (ord(b) - ord(a)) % 10 == 0 else ''
输出:
"\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d"
"\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17"
"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21"
"\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b"
"\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35"
"\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49"
"\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53"
"\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d"
"\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67"
"\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71"
"\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b"
"\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85"
"\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99"
"\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3"
"\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad"
"\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
"\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1"
"\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb"
"\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5"
"\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9"
"\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3"
"\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd"
"\xfe"