1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Rep Power

    Question Problem working with bitmasks


    I have some binary data and I want to see if a certain bit is set and then take action.
    I have something like this:
    		for i in xrange(0, length):
    			corr = fh.read(1)
    			mac = fh.read(6)[-3:]
    			print hexlify(corr) +  " - " + hexlify(mac)
    			if (int(hexlify(corr),16))&0x01:
    				print "DELETE NODE " + hexlify(mac)
    				#print delete_node(mac)
    			if (int(hexlify(corr),16))&0x02:
    				print "NEW INFO " + hexlify(mac)
    				#print new_info(mac)
    			if (int(hexlify(corr),16))&0x04:
    				print "NEW MAC "+ hexlify(mac)
    				#print new_mac(mac)
    			if (int(hexlify(corr),16))&0x08:
    				print "NEW FW " + hexlify(mac)
    And I really don't like how I first convert the number into a string with hexlify and then convert it back into an int base 16 just to apply the bitmask and it doesn't seem to work when corr = 0x10 anyways. How can I improve this and make it work?
    Oh and fh is a StringIO object.

    Thank you!
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    hexlify is somewhere in here

    Is int(hexlify(corr),16) not identical to corr ?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo