443,965 Members | 1,450 Online
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

# How to convert a number to binary?

 P: n/a Converting binary to base 10 is easy: >>int('11111111', 2) 255 Converting base 10 number to hex or octal is easy: >>oct(100) '0144' >>hex(100) '0x64' Is there an *easy* way to convert a number to binary? May 17 '07 #1
15 Replies

 P: n/a On May 17, 2007, at 6:33 PM, Lyosha wrote: Converting binary to base 10 is easy: >>>int('11111111', 2) 255 Converting base 10 number to hex or octal is easy: >>>oct(100) '0144' >>>hex(100) '0x64' Is there an *easy* way to convert a number to binary? def to_base(number, base): 'converts base 10 integer to another base' number = int(number) base = int(base) if base < 2 or base 36: raise ValueError, "Base must be between 2 and 36" if not number: return 0 symbols = string.digits + string.lowercase[:26] answer = [] while number: number, remainder = divmod(number, base) answer.append(symbols[remainder]) return ''.join(reversed(answer)) Hope this helps, Michael --- "I would rather use Java than Perl. And I'd rather be eaten by a crocodile than use Java."  Trouser May 17 '07 #2

 P: n/a On May 17, 4:40 pm, Michael Bentley >int('11111111', 2) 255 Converting base 10 number to hex or octal is easy: >>oct(100) '0144' >>hex(100) '0x64' Is there an *easy* way to convert a number to binary? def to_base(number, base): 'converts base 10 integer to another base' number = int(number) base = int(base) if base < 2 or base 36: raise ValueError, "Base must be between 2 and 36" if not number: return 0 symbols = string.digits + string.lowercase[:26] answer = [] while number: number, remainder = divmod(number, base) answer.append(symbols[remainder]) return ''.join(reversed(answer)) Hope this helps, Michael That's way too complicated... Is there any way to convert it to a one- liner so that I can remember it? Mine is quite ugly: "".join(str((n/base**i) % base) for i in range(20) if n>=base**i) [::-1].zfill(1) May 17 '07 #3

 P: n/a On May 17, 6:45 pm, Lyosha >>int('11111111', 2) 255 Converting base 10 number to hex or octal is easy: >>>oct(100) '0144' >>>hex(100) '0x64' Is there an *easy* way to convert a number to binary? def to_base(number, base): 'converts base 10 integer to another base' number = int(number) base = int(base) if base < 2 or base 36: raise ValueError, "Base must be between 2 and 36" if not number: return 0 symbols = string.digits + string.lowercase[:26] answer = [] while number: number, remainder = divmod(number, base) answer.append(symbols[remainder]) return ''.join(reversed(answer)) Hope this helps, Michael That's way too complicated... Is there any way to convert it to a one- liner so that I can remember it? Mine is quite ugly: "".join(str((n/base**i) % base) for i in range(20) if n>=base**i) [::-1].zfill(1)- Get the gmpy module (note inconsistencies involving octal and hex): >>import gmpyfor base in xrange(2,37): print gmpy.digits(255,base) 11111111 100110 3333 2010 1103 513 0377 313 255 212 193 168 143 120 0xff f0 e3 d8 cf c3 bd b2 af a5 9l 9c 93 8n 8f 87 7v 7o 7h 7a 73 May 18 '07 #4

 P: n/a On May 17, 2007, at 6:45 PM, Lyosha wrote: On May 17, 4:40 pm, Michael Bentley On May 17, 2007, at 6:33 PM, Lyosha wrote: >>Converting binary to base 10 is easy:>int('11111111', 2)255 >>Converting base 10 number to hex or octal is easy:>oct(100)'0144'>hex(100)'0x64' >>Is there an *easy* way to convert a number to binary? def to_base(number, base): 'converts base 10 integer to another base' number = int(number) base = int(base) if base < 2 or base 36: raise ValueError, "Base must be between 2 and 36" if not number: return 0 symbols = string.digits + string.lowercase[:26] answer = [] while number: number, remainder = divmod(number, base) answer.append(symbols[remainder]) return ''.join(reversed(answer))Hope this helps,Michael That's way too complicated... Is there any way to convert it to a one- liner so that I can remember it? Mine is quite ugly: "".join(str((n/base**i) % base) for i in range(20) if n>=base**i) [::-1].zfill(1) to_base(number, 2) is too complicated? May 18 '07 #5

 P: n/a On May 17, 6:45 pm, Lyosha =base**i)[::-1].zfill(1) Howzis? "".join(map(str,[ int(bool(n & 2**i)) for i in range(20) if n>2**i ] [::-1])) Uses: - integer & to test for bit high or low, returns 2**i - bool(int) to evaluate boolean value of integers - zero -False, nonzero -True - int(bool) to convert True->1 and False->0 -- Paul May 18 '07 #6

 P: n/a On May 17, 6:45 pm, Lyosha =base**i)[::-1].zfill(1) Howzis? "".join(map(str,[ int(bool(n & 2**i)) for i in range(20) if n>2**i ] [::-1])) Uses: - integer & to test for bit high or low, returns 2**i - bool(int) to evaluate boolean value of integers - zero -False, nonzero -True - int(bool) to convert True->1 and False->0 -- Paul May 18 '07 #7

 P: n/a "".join([('0','1')[bool(n & 2**i)] for i in range(20) if n>2**i] [::-1]) Still only valid up to 2**20, though. -- Paul May 18 '07 #8

 P: n/a Lyosha schrieb: On May 17, 4:40 pm, Michael Bentley >On May 17, 2007, at 6:33 PM, Lyosha wrote: >>>Converting binary to base 10 is easy:>>int('11111111', 2)255 >>>Converting base 10 number to hex or octal is easy:>>oct(100)'0144'>>hex(100)'0x64' >>>Is there an *easy* way to convert a number to binary? def to_base(number, base): 'converts base 10 integer to another base' number = int(number) base = int(base) if base < 2 or base 36: raise ValueError, "Base must be between 2 and 36" if not number: return 0 symbols = string.digits + string.lowercase[:26] answer = [] while number: number, remainder = divmod(number, base) answer.append(symbols[remainder]) return ''.join(reversed(answer))Hope this helps,Michael That's way too complicated... Is there any way to convert it to a one- liner so that I can remember it? Mine is quite ugly: "".join(str((n/base**i) % base) for i in range(20) if n>=base**i) [::-1].zfill(1) Wrote this a few moons ago:: dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else '' Regards, Stargaming May 18 '07 #9

 P: n/a Lyosha

 P: n/a On May 17, 11:04 pm, Stargaming >Is there an *easy* way to convert a number to binary? [...] > Wrote this a few moons ago:: dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else '' This is awesome. Exactly what I was looking for. Works for other bases too. I guess the reason I couldn't come up with something like this was being brainwashed that lambda is a no-no. And python2.5 funky ?: expression comes in handy! Thanks a lot! May 18 '07 #11

 P: n/a On May 17, 11:10 pm, Ben Finney wrote: [...] That's way too complicated... Is there any way to convert it to a one- liner so that I can remember it? You put in a module so you don't *have* to remember it. Then, you use it in this one-liner: foo = to_base(15, 2) Carrying a whole lot of one-liners around in your head is a waste of neurons. Neurons are far more valuable than disk space, screen lines, or CPU cycles. While I agree with this general statement, I think remembering a particular one-liner to convert a number to a binary is more valuable to my brain than remembering where I placed the module that contains this function. I needed the one-liner not to save disk space or screen lines. It's to save time, should I need to convert to binary when doing silly little experiments. I would spend more time getting the module wherever it is I stored it (and rewriting it if it got lost). It's fun, too. May 18 '07 #12

 P: n/a Lyosha >>Is there an *easy* way to convert a number to binary? [...] Wrote this a few moons ago:: dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else '' This is awesome. Exactly what I was looking for. Works for other bases too. Just don't pass it a negative number ;-) -- Nick Craig-Wood

 P: n/a Lyosha On May 17, 11:04 pm, Stargaming dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else '' [ ... ]I guess the reason I couldn't come up with something like this wasbeing brainwashed that lambda is a no-no.And python2.5 funky ?: expression comes in handy! def dec2bin(x): return x and (dec2bin(x/2)+str(x%2)) or '' does the same job without lambda or Python 2.5 (and note that the usual warning about a and b or c doesn't apply here as b is guaranteed to evaluate as true). -- \S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/ "Frankly I have no feelings towards penguins one way or the other" -- Arthur C. Clarke her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump May 18 '07 #14

 P: n/a Lyosha wrote: On May 17, 11:10 pm, Ben Finney wrote: [...] >>That's way too complicated... Is there any way to convert it to aone- liner so that I can remember it? You put in a module so you don't *have* to remember it.Then, you use it in this one-liner: foo = to_base(15, 2)Carrying a whole lot of one-liners around in your head is a waste ofneurons. Neurons are far more valuable than disk space, screen lines,or CPU cycles. While I agree with this general statement, I think remembering a particular one-liner to convert a number to a binary is more valuable to my brain than remembering where I placed the module that contains this function. I needed the one-liner not to save disk space or screen lines. It's to save time, should I need to convert to binary when doing silly little experiments. I would spend more time getting the module wherever it is I stored it (and rewriting it if it got lost). It's fun, too. Id use the "silly little module" even for experiments. Most Python programmers keep a directory full of such "stock" modules. regards Steve May 18 '07 #15

 P: n/a In article <11*********************@l77g2000hsb.googlegroups. com>, Lyosha

### This discussion thread is closed

Replies have been disabled for this discussion.