Skip to content

Instantly share code, notes, and snippets.

@sim642
Last active August 29, 2015 14:24
Show Gist options
  • Save sim642/f978cf080663fac31119 to your computer and use it in GitHub Desktop.
Save sim642/f978cf080663fac31119 to your computer and use it in GitHub Desktop.
Integer to base 256 byte string and vice versa
def int2bytes(n):
str = ''
while n:
str = chr(n % 256) + str
n = n // 256
return str if len(str) > 0 else '\0'
def bytes2int(str):
n = 0
for c in str:
n = n * 256 + ord(c)
return n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment