Skip to content

Instantly share code, notes, and snippets.

@wofeiwo
Created December 13, 2012 05:38
Show Gist options
  • Save wofeiwo/4274306 to your computer and use it in GitHub Desktop.
Save wofeiwo/4274306 to your computer and use it in GitHub Desktop.
hex,string,int_bits transfer
def hexstr(s):
"""
turns ABCD
into
41424344
"""
tmp=[]
for c in s:
tmp+=["%2.2x"%ord(c)]
return "".join(tmp)
def str2int_bits(bits, s):
nchars = bits / 8
r = 0
for i in range(0, nchars):
#print "%d = %x << %d" % (ord(s[i]) << 8*i, ord(s[i]), 8*i)
r += ord(s[nchars-i-1]) << 8*i
return r
def int2hexs(myint):
str=""
a=int(myint % 256)
myint=myint >> 8
b=int(myint % 256)
myint=myint >> 8
c=int(myint % 256)
myint=myint >> 8
d=int(myint % 256)
str+="%c%c%c%c" % (d,c,b,a)
return str
def short2hexs(halfword):
str=""
a = halfword & 0xff
b = halfword/256 & 0xff
str+="%c%c" % (b,a)
return str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment