Last active
August 29, 2015 14:24
-
-
Save sim642/f978cf080663fac31119 to your computer and use it in GitHub Desktop.
Integer to base 256 byte string and vice versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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