Created
December 13, 2012 05:38
-
-
Save wofeiwo/4274306 to your computer and use it in GitHub Desktop.
hex,string,int_bits transfer
This file contains 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 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