Python迭代

tho*_*yon 3 python hex loops for-loop increment

我正在尝试在Python中创建一个简单的脚本,它将打印十六进制值并增加值,如下所示:

 char = 0
 char2 = 0

 def doublehex():
    global char,char2
    for x in range(255):
        char = char + 1
        a = str(chr(char)).encode("hex")
        for p in range(255):
           char2 = char2 + 1
           b = str(chr(char2)).encode("hex")
           c = a+" "+b
           print "testing with:%s"%(c)
doublehex()
Run Code Online (Sandbox Code Playgroud)

输出:

testing with:01 01
testing with:01 02
testing with:01 03
[snip]
testing with:01 fd
testing with:01 fe
testing with:01 ff

Traceback (most recent call last):
  File "test2.py", line 16, in doublehex
   b = str(chr(char2)).encode("hex")
ValueError: chr() arg not in range(256)
Run Code Online (Sandbox Code Playgroud)

其实我要做的是:

01 01
01 02
[snip]
01 ff
02 01
02 02
Run Code Online (Sandbox Code Playgroud)

等等,直到ff ff.我的剧本有什么问题?

我似乎无法尝试:

00 01
00 02
Run Code Online (Sandbox Code Playgroud)

我不知道为什么.

小智 6

for x in xrange(256):
    for y in xrange(256):
        print '%02x %02x' % (x, y)
Run Code Online (Sandbox Code Playgroud)