Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # v0.1: initial version. Only supported integers 0 - 1023
- # v0.2: Added arg to function to support integers beyond 1023
- # v0.3: Now it works with any integer that python can chew.
- # v0.3.1: Bugfix. b10_4(1) would crash, so I added a special case for anything below 10 (decimal 4)
- # v0.3.2: Bug affects any power of 4. So I changed the logic to fix that. < to <=
- # v0.3.3: Added test() function and added default arg for b10_4()
- def test(r=0x1000):
- for i in range(r):
- print("%X of %X:\t%s" % (i,r,b10_4(i)))
- def b10_4(N=0):
- if N < 4: return str(N)
- n = 0
- i = 0
- outs = ""
- while 4**n <= N: n += 1
- out = [0 for j in range(n)]
- while n >= 0:
- while N >= 4**n:
- N -= 4**n
- out[n] += 1
- while N < 4**n:
- n -= 1
- out.reverse()
- while i < len(out):
- outs += str(out[i])
- i += 1
- return outs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement