Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python iteration

I'm trying to do a simple script in Python that will print hex values and increment value like this:

 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()

Output:

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)

Actually what I'm trying to do is:

01 01
01 02
[snip]
01 ff
02 01
02 02

And so on, until ff ff. What's wrong in my script?

Also it seems I can't try:

00 01
00 02

I don't know why.

like image 686
thomytheyon Avatar asked Sep 16 '26 00:09

thomytheyon


1 Answers

for x in xrange(256):
    for y in xrange(256):
        print '%02x %02x' % (x, y)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!