Quantcast
Channel: Convert int to binary string in Python - Stack Overflow
Browsing index pages (74 articles)

Convert int to binary string in Python

How do I convert an integer into a binary string in Python?37 →'100101'

View Article


Answer by Van Gale for Convert int to binary string in Python

Unless I'm misunderstanding what you mean by binary string I think the module you are looking for is struct

View Article


Answer by Tung Nguyen for Convert int to binary string in Python

Python's string format method can take a format spec. >>> "{0:b}".format(37)'100101'Format spec docs for Python 2Format spec docs for Python 3

View Article

Answer by John Fouhy for Convert int to binary string in Python

If you're looking for bin() as an equivalent to hex(), it was added in python 2.6.Example:>>> bin(10)'0b1010'

View Article

Answer by paxdiablo for Convert int to binary string in Python

Python actually does have something already built in for this, the ability to do operations such as '{0:b}'.format(42), which will give you the bit pattern (in a string) for 42, or 101010.For a more...

View Article


Answer by kctong529 for Convert int to binary string in Python

As a reference:def toBinary(n): return ''.join(str(1 & int(n) >> i) for i in range(64)[::-1])This function can convert a positive integer as large as 18446744073709551615, represented as...

View Article

Answer by Martin Thoma for Convert int to binary string in Python

If you want a textual representation without the 0b-prefix, you could use this:get_bin = lambda x: format(x, 'b')print(get_bin(3))>>> '11'print(get_bin(-3))>>> '-11'When you want a...

View Article

Answer by quents for Convert int to binary string in Python

Here is the code I've just implemented. This is not a method but you can use it as a ready-to-use function!def inttobinary(number): if number == 0: return str(0) result ="" while (number != 0):...

View Article


Answer by Kyle Siopiolosz for Convert int to binary string in Python

Along a similar line to Yusuf Yazici's answerdef intToBin(n): if(n < 0): print "Sorry, invalid input." elif(n == 0): print n else: result = "" while(n != 0): result += str(n%2) n /= 2 print...

View Article


Answer by Chandler for Convert int to binary string in Python

Somewhat similar solutiondef to_bin(dec): flag = True bin_str = '' while flag: remainder = dec % 2 quotient = dec / 2 if quotient == 0: flag = False bin_str += str(remainder) dec = quotient bin_str =...

View Article

Answer by user210021 for Convert int to binary string in Python

here is simple solution using the divmod() fucntion which returns the reminder and the result of a division without the fraction.def dectobin(number): bin = '' while (number >= 1): number, rem =...

View Article

Answer by mukundan for Convert int to binary string in Python

def binary(decimal) : otherBase = "" while decimal != 0 : otherBase = str(decimal % 2) + otherBase decimal //= 2 return otherBaseprint binary(10)output:1010

View Article

Answer by Reza Abtin for Convert int to binary string in Python

Yet another solution with another algorithm, by using bitwise operators. def int2bin(val): res='' while val>0: res += str(val&1) val=val>>1 # val=val/2 return res[::-1] # reverse the...

View Article


Answer by dblaze for Convert int to binary string in Python

n=input()print(bin(n).replace("0b", ""))

View Article

Answer by Bob Stein for Convert int to binary string in Python

Summary of alternatives:n=42assert "-101010" == format(-n, 'b')assert "-101010" == "{0:b}".format(-n)assert "-101010" == (lambda x: x >= 0 and str(bin(x))[2:] or "-"+ str(bin(x))[3:])(-n)assert...

View Article


Answer by Aziz Alto for Convert int to binary string in Python

one-liner with lambda:>>> binary = lambda n: '' if n==0 else binary(n/2) + str(n%2)test:>>> binary(5)'101'EDIT: but then :( t1 = time()for i in range(1000000): binary(i)t2 =...

View Article

Answer by pitfall for Convert int to binary string in Python

Using numpy pack/unpackbits, they are your best friends.Examples-------->>> a = np.array([[2], [7], [23]], dtype=np.uint8)>>> aarray([[ 2], [ 7], [23]], dtype=uint8)>>> b =...

View Article


Answer by ergonaut for Convert int to binary string in Python

Here's yet another way using regular math, no loops, only recursion. (Trivial case 0 returns nothing).def toBin(num): if num == 0: return "" return toBin(num//2) + str(num%2)print ([(toBin(i)) for i in...

View Article

Answer by Xiang for Convert int to binary string in Python

A simple way to do that is to use string format, see this page.>> "{0:b}".format(10)'1010'And if you want to have a fixed length of the binary string, you can use this:>>...

View Article

Answer by kcrisman for Convert int to binary string in Python

If you are willing to give up "pure" Python but gain a lot of firepower, there is Sage - example here:sage: a = 15sage: a.binary()'1111'You'll note that it returns as a string, so to use it as a number...

View Article
Browsing index pages (74 articles)


Latest Images