Working with hexadecimal, binaries, and other bases is becoming common at work, at first we try to use custom functions to convert from and to some bases, especially hexadecimal. But other languages have built-in methods to do that conversion, and so python too.
First to convert and hexadecimal string to an integer we use:
int_number = int('ab1', 16)The variable int_number will be 2737. That is ab1 hex to integer conversion. Now what about the other way, integer to hexadecimal, that would be:
hex(int_number)
And that would return '0xab1'. Note that it returns a string and has the 0x notation, we could use that notation also in the first conversion like this:
int_number = int('0xab1', 16)So as you can see we pass a parameter to the int function to indicate the base, if we dont pass any python assumes that we are using a 10 base. As you have also seen there is a hex built-in function but there is no binary, or any other base built-in, so since we work a lot with binary we built a small function to do the work. There are hundreds of ways to do this, using recursion, whiles, fors, lamdas, etc. We end up using this function:
def dectobin(number):
if number < 1:
return ""
else:
return dectobin(number / 2) + str(number & 1)
Hope this works for you.
14 comments:
Functional programming ftw!
def newbin_listcomp(number):
if number < 0: raise ValueError, 'negative number zomg zomg'
return ''.join([{'0':'000', '1':'001', '2':'010', '3':'011', '4':'100', '5':'101', '6':'110', '7':'111'}[ocd] for ocd in '%o' % number]).lstrip('0') or 0
this one is much much faster
xbitotech.blogspot.com; You saved my day again.
Nice fill someone in on and this post helped me alot in my college assignement. Gratefulness you as your information.
Amiable dispatch and this enter helped me alot in my college assignement. Thank you on your information.
Well your article helped me altogether much in my college assignment. Hats off to you dispatch, will look progressive for more related articles soon as its sole of my favourite issue to read.
firewall gold -
fitness model program -
fit over 40 -
fit yummy yummy -
flatten your abs -
forex auto pilot -
gain opinion -
gold secrets -
government records -
governmentregistry -
government registry -
green diy energy -
healthy urban kitchen -
highest pay surveys -
holo think -
homemadeenergy -
home made energy -
i want a teaching job -
joyful tomato -
keyword elite -
learn photoshop videos -
lmt forex formula -
lower body make over -
macro virus -
malware bot -
maternityacupressure -
maternity acupressure -
meet your sweet -
microcap millionaires -
moles warts removal -
money siphon system -
muscle gaining secrets -
my list strategy -
my phone riches -
natural cancer treatments -
online tv pc -
panic away -
pdf creator -
peel away ads -
pips leader -
ppc web spy -
prevent sweating -
privacy control -
publicrecordspro -
public records pro -
questions for couples -
quick article pro -
quick paid surveys -
quit smoking today -
reg defense -
registry winner -
reg tool -
reverse mobile -
richard mackenzie direct -
rocket german -
rotator cuff training -
rss feeds submit -
satellite pc box -
save the marriage -
spy no more -
spyware stop -
super seduction power -
surefire trading challenge -
tattoo me now -
the bad breath report -
thedietsolutionprogram -
the diet solution program -
the dog food conspiracy -
the mini site formula -
tmj help -
tonsil stones remedies -
truth about abs -
twitter trick -
vincedelmontefitness -
vince del monte fitness -
warp speed fat loss -
wedding speech 4u -
win clear -
xbox 360 red light fix -
xp repair pro -
500 love making tips -
acid alkaline diet -
advanced defrag -
alpha male system -
amazing cover letters -
anti spyware -
anti spyware bot -
article rewriter -
art of approaching -
auto cash system -
burnthefat -
burn the fat -
bv cures -
calling men -
camera dollars -
carb rotation diet -
cash making power sites -
cb mall -
combat the fat -
conversationalhypnosis -
conversational hypnosis -
credit secrets bible -
homemadeenergy -
home made energy -
hybrid water power -
i software tv -
joyful tomato -
keyword spy pro -
kingdom of pets -
macro virus -
malware bot -
maternityacupressure -
maternity acupressure -
meet your sweet -
microcap millionaires -
moles warts removal -
mole wart removal -
muscle gaining secrets -
natural cure for yeast infection -
paid surveys etc -
panic away -
pdf creator -
public records pro -
quick article pro -
quick income blueprint -
quit smoking today -
ranking loophole -
reg clean -
reg defense -
regi cleanse -
registry easy -
registry genius -
registry winner -
reg sweep -
reg tool -
reverse mobile -
richard mackenzie direct -
rig worker -
rocket chinese -
rocket french -
rocket german -
rotator cuff training -
rss feeds submit -
save the marriage -
site builder elite -
six figure yearly 2009 -
sleep tracks -
sold in 21 days -
spyware stop -
stop being tired -
stop your hair loss -
the bad breath report -
thedietsolutionprogram -
the diet solution program -
tonsil stones remedies -
top secret magic code -
truth about abs -
turbulence training -
turn your photos into cash -
vincedelmontefitness -
vince del monte fitness -
warp speed fat loss -
wedding speech 4u -
win clear -
xbox 360 red light fix -
xp repair pro -
1st promotion -
3 redlight fix -
500 love making tips -
acid alkaline diet -
PPC Ad
very handy, thanx a lot for this blog ........ This is exactly what I was looking for.
Hello people want to express my satisfaction with this blog very creative and I really like the views of the focus very good indeed Thank you for the helpful information. I hope you keep up the good work on making your blog a success!
gf tits
gf boobs
girlfriend tits
girlfriend boobs
gfs huge boobs
gfs huge tits
natural tits
busty amateur
juicy boobies
girlfriend orgasms
girlfriend sexy
girlfriend amateur
gfs cute
gfs homemade
hottest tens
gfs young
real girlfriend
slut girlfriend
dirty gfs
jizz on my gf
gfs cum face
gfs cumshots
gfs creampies
gfs cum
jizz girlfriends
jizz gfs
girlfriend facials
gfs facial
Post a Comment