Skip to content

Instantly share code, notes, and snippets.

@heikoheiko
Created March 23, 2014 19:00
Show Gist options
  • Save heikoheiko/9727917 to your computer and use it in GitHub Desktop.
Save heikoheiko/9727917 to your computer and use it in GitHub Desktop.
def int_to_big_endian(integer):
'''convert a integer to big endian binary string'''
if integer == 0:
return ''
s = '%x' % integer
if len(s) & 1:
s = '0' + s
return s.decode('hex')
def big_endian_to_int(string):
'''convert a big endian binary string to integer'''
string = string or '\x00' # cpp client encodes \x00 as ''
s = string.encode('hex')
return int(s, 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment