797

How do I convert an integer into a binary string in Python?

37   →   '100101'
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Nate
  • 18,892
  • 27
  • 70
  • 93
  • For the opposite take, for a pure string processing algorithm, see [this](https://stackoverflow.com/a/60693697/8929814). – CopyPasteIt Mar 15 '20 at 17:05

36 Answers36

1058

Python's string format method can take a format spec.

>>> "{0:b}".format(37)
'100101'

Format spec docs for Python 2

Format spec docs for Python 3

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
Tung Nguyen
  • 12,655
  • 4
  • 19
  • 10
  • 152
    `str.format()` just to format one value is overkill. Go straight to the [`format()` function](https://docs.python.org/2/library/functions.html#format): `format(n, 'b')`. There is no need to parse out the placeholder and match it to an argument, go straight for the value formatting operation itself. Only use `str.format()` if you need to place the formatted result in a longer string (e.g. use it as a template). – Martijn Pieters Dec 10 '15 at 10:23
  • 57
    @mike: Or use the formatting specification. Add the number of digits with a leading `0` to the formatting string: `format(10, '016b')` formats to 16 digits with leading zeros. – Martijn Pieters Dec 10 '15 at 10:24
  • In this case the `0` in `"{0:b}"` can be dropped no? I mean, in the case where only one number is being formatted, it is correct to put `"{:b}"`, isn't it? – tomasyany Jul 28 '16 at 23:53
  • 7
    typically one would use 4/8/... bit representation: `"{:08b}".format(37)` – Sparkler Aug 14 '18 at 21:51
  • 19
    f"{37:b}" in Python3.7 or later. – D. A. Jul 18 '19 at 04:30
  • 5
    There is an issue here with negative numbers. @nate didn't clearly specify what the desired output was in that case, but purely in binary numbers, negative signs don't exist. So the most significant bit is generally used for the negative sign. Assuming we use 8bit integers, -37 would be `0b10100101`. But with an unsigned integer, that value would be 165. So it is not this simple. The answer should reflect this. – Dolf Andringa Jun 25 '20 at 04:18
646

If you're looking for bin() as an equivalent to hex(), it was added in python 2.6.

Example:

>>> bin(10)
'0b1010'
Akseli Palén
  • 27,244
  • 10
  • 65
  • 75
John Fouhy
  • 41,203
  • 19
  • 62
  • 77
  • 102
    Note also that it's faster to do `str(bin(i))[2:]` (0.369s for 1000000ops) than `"{0:b}".format(i)` (0.721s for 1000000ops) – mVChr Oct 30 '13 at 07:55
  • 89
    @mVChr if someone's converting numbers into an ASCII binary representation, I really hope speed doesn't matter. – Nick T Feb 05 '14 at 05:04
  • 41
    @mVChr: `str.format()` is the wrong tool anyway, you would use `format(i, 'b')` instead. Take into account that that also gives you padding and alignment options though; `format(i, '016b')` to format to a 16-bit zero-padded binary number. To do the same with `bin()` you'd have to add a `str.zfill()` call: `bin(i)[2:].zfill(16)` (no need to call `str()`!). `format()`'s readability and flexibility (dynamic formatting is much harder with `bin()`) are great tradeoffs, don't optimise for performance unless you have to, until then optimise for maintainability. – Martijn Pieters Dec 10 '15 at 10:28
  • What does [2:] mean? – zero_cool Aug 18 '17 at 20:43
  • 13
    Of course, with python 3.6+ you can now use `f"{37:b}"`. – Luke Davis Nov 06 '17 at 06:48
72

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 general philosophy, no language or library will give its user base everything that they desire. If you're working in an environment that doesn't provide exactly what you need, you should be collecting snippets of code as you develop to ensure you never have to write the same thing twice. Such as, for example, the pseudo-code:

define intToBinString, receiving intVal:
    if intVal is equal to zero:
        return "0"
    set strVal to ""
    while intVal is greater than zero:
        if intVal is odd:
            prefix "1" to strVal
        else:
            prefix "0" to strVal
        divide intVal by two, rounding down
    return strVal

which will construct your binary string based on the decimal value. Just keep in mind that's a generic bit of pseudo-code which may not be the most efficient way of doing it though, with the iterations you seem to be proposing, it won't make much difference. It's really just meant as a guideline on how it could be done.

The general idea is to use code from (in order of preference):

  • the language or built-in libraries.
  • third-party libraries with suitable licenses.
  • your own collection.
  • something new you need to write (and save in your own collection for later).
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Some good advise in this answer. Only too bad that the code is needlessly slow. You propose an O(N^2) algo where an O(N) would do. The problematic part is in the `s = "1" + s` and `s = "0" + s` lines. Each makes an unnecessary copy of s. You should reverse the string just before you return it instead. – Andreas Magnusson Jan 09 '20 at 11:31
  • @Andreas, what I *proposed* was to use `'{0:b}'.format(42)`, the slow method was simply an example of how to do it generically, which may or may not be O(n^2) depending on the actual language used. It only looks like Python since Python is an ideal pseudo-code language so I'll change that to make it clear. – paxdiablo Jan 09 '20 at 13:25
  • Actually it would be a pretty esoteric language where `s = "1" + s` wasn't O(N) when `s` is a string type. Maybe a language where all strings are stored backwards or each char is a node in a linked list? For any typical language a string is basically an array of chars. In that case prefixing a string requires that a copy is made, how else are you going to put the character before the other characters? – Andreas Magnusson Jan 09 '20 at 13:28
  • I can easily envisage a string type that consists of a block of memory where the string is right-justified within that block, and an offset to its starting character. To prefix a character, you would then simply reduce the offset and store the character there. Yes, that *would* be esoteric but it makes little sense to me to argue about possible real-world issues with a bit of pseudo-code, *especially* since you're not likely to have more than a few dozen bits/iterations. Even the much maligned bubble sort is adequate if your data size is small :-) In any case, I'll add a note about efficiency. – paxdiablo Jan 09 '20 at 15:11
  • Sure, if efficiency is important you'd probably not choose python to begin with. Still In my experience it happens quite often that code that was written naïvely using an O(N²) algo and tested with a small data set quickly gets used with a much larger data set because "it seems to work". Then all of a sudden you have code that takes hours to run that when fixed may take only seconds. O(N²) algos are insidious because they seem to work for a while but when your data scales they don't and by then the guy who wrote them has quit and no one knows why things take forever. – Andreas Magnusson Jan 09 '20 at 18:30
  • Instead of the if-else-statements you could add the reminder directly: prefix (intVal%2) to strVal I know this thread is more than three years old and I don't write this to argue. I like breaking up algorithms into simplified examples. This is just my two cents. :-) – Thomas Gabrielsen Feb 26 '23 at 21:00
62

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 n-bit representation:

get_bin = lambda x, n: format(x, 'b').zfill(n)
>>> get_bin(12, 32)
'00000000000000000000000000001100'
>>> get_bin(-12, 32)
'-00000000000000000000000000001100'

Alternatively, if you prefer having a function:

def get_bin(x, n=0):
    """
    Get the binary representation of x.

    Parameters
    ----------
    x : int
    n : int
        Minimum number of digits. If x needs less digits in binary, the rest
        is filled with zeros.

    Returns
    -------
    str
    """
    return format(x, 'b').zfill(n)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 8
    Or just use `format(integer, 'b')`. `bin()` is a debugging tool, specifically aimed at producing the *Python binary integer literal syntax*, `format()` is meant to produce specific formats. – Martijn Pieters Dec 10 '15 at 10:21
  • 1
    @MartijnPieters Thank you very much for mentioning it. I've adjusted my solutution. How do you know that `bin()` is a debugging tool aimed at producing the Python binary integer literal syntax? I couldn't find that in the documentation. – Martin Thoma Dec 10 '15 at 10:36
  • 3
    From the documentation: *The result is a valid Python expression*. It's aim is to produce a Python expression, not to produce end-user representations. The same applies to `oct()` and `hex()`. – Martijn Pieters Dec 10 '15 at 10:37
  • 6
    More alternatives: If you are going to make the width dynamic, instead of `str.zfill()` you could use `str.format()` or `format()` with a dynamic second argument: `'{0:0{1}b}'.format(x, n)` or `format(b, '0{}b'.format(n))`. – Martijn Pieters Dec 10 '15 at 10:41
  • @MartijnPieters Wow, thank you very much for this input! I didn't know that this was possible with format. However, I think my current answer with `zfill` is easier to read and understand than the dynamic second argument, so I'll keep that. – Martin Thoma Dec 10 '15 at 10:45
  • Which is why I called it *more alternatives*. :-) – Martijn Pieters Dec 10 '15 at 10:46
  • `python -m timeit "format(255, 'b')"`: 154 nsec per loop. `python -m timeit "bin(255)[2:]"`: 130 nsec per loop. – Cees Timmerman Oct 05 '20 at 16:58
  • @CeesTimmerman Such micro-benchmarks are super hard to get right. For me, the first one gives 170ns and the second one 176ns – Martin Thoma Oct 05 '20 at 18:25
  • @MartinThoma On my Intel Core i7 with Windows 10 and Python 3.8.5-64 `bin` is consistently 20% faster, and on my AMD Ryzen 3 with Windows 10 and Python 3.7.4-32, 30%. – Cees Timmerman Oct 05 '20 at 18:48
60

I am surprised there is no mention of a nice way to accomplish this using formatting strings that are supported in Python 3.6 and higher. TLDR:

>>> number = 1
>>> f'0b{number:08b}'
'0b00000001'

Longer story

This is functionality of formatting strings available from Python 3.6:

>>> x, y, z = 1, 2, 3
>>> f'{x} {y} {2*z}'
'1 2 6'

You can request binary as well:

>>> f'{z:b}'
'11'

Specify the width:

>>> f'{z:8b}'
'      11'

Request zero padding:

f'{z:08b}'
'00000011'

And add common prefix to signify binary number:

>>> f'0b{z:08b}'
'0b00000011'

You can also let Python add the prefix for you but I do not like it so much as the version above because you have to take the prefix into width consideration:

>>> f'{z:#010b}'
'0b00000011'

More info is available in official documentation on Formatted string literals and Format Specification Mini-Language.

Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28
  • To add underscores: `f'0b{z:09_b}'` => `'0b0000_0011'` – Frederic Oct 23 '20 at 10:34
  • what about endianness? can one change it? – lesolorzanov Oct 26 '20 at 11:24
  • This is out of scope of this question. The most significant first is the canonical way how to write number in positional system regardless of system endianness which is just an implementation detail. You can do `f'{z:08b}'[::-1]` to achieve the least significant byte first ordering, however this will IMHO in most cases cause just confusion... – Roman Pavelka Oct 26 '20 at 18:08
  • 1
    f strings also appear to be faster than format(). `timeit.timeit( 'f"{2:08b}"', number=10000000 ) => 1.1823169720000806` versus `timeit.timeit( 'format(2,"08b")', number=10000000 ) => 1.3507722609992925` – Terrance Aug 05 '22 at 16:23
44

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 string '1111111111111111111111111111111111111111111111111111111111111111'.

It can be modified to serve a much larger integer, though it may not be as handy as "{0:b}".format() or bin().

kctong529
  • 938
  • 8
  • 10
38

This is for python 3 and it keeps the leading zeros !

print(format(0, '08b'))

enter image description here

grepit
  • 21,260
  • 6
  • 105
  • 81
30

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:

>> "{0:{fill}8b}".format(10, fill='0')
'00001010'

If two's complement is required, then the following line can be used:

'{0:{fill}{width}b}'.format((x + 2**n) % 2**n, fill='0', width=n)

where n is the width of the binary string.

Xiang
  • 411
  • 4
  • 4
17

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 = time()
print(t2 - t1)
# 6.57236599922

in compare to

t1 = time()
for i in range(1000000):
    '{0:b}'.format(i)
t2 = time()
print(t2 - t1)
# 0.68017411232
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60
16

As the preceding answers mostly used format(), here is an f-string implementation.

integer = 7
bit_count = 5
print(f'{integer:0{bit_count}b}')

Output:

00111

For convenience here is the python docs link for formatted string literals: https://docs.python.org/3/reference/lexical_analysis.html#f-strings.

John Forbes
  • 1,248
  • 14
  • 19
12

Summary of alternatives:

n=42
assert  "-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 "0b101010" == bin(n)
assert   "101010" == bin(n)[2:]   # But this won't work for negative numbers.

Contributors include John Fouhy, Tung Nguyen, mVChr, Martin Thoma. and Martijn Pieters.

Community
  • 1
  • 1
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
  • 7
    `str.format()` just to format one value is overkill. Go straight to the `format()` function: `format(n, 'b')`. No need to parse out the placeholder and match it to an argument that way. – Martijn Pieters Dec 10 '15 at 10:23
11
>>> format(123, 'b')
'1111011'
Sandu Ursu
  • 1,181
  • 1
  • 18
  • 28
10

For those of us who need to convert signed integers (range -2**(digits-1) to 2**(digits-1)-1) to 2's complement binary strings, this works:

def int2bin(integer, digits):
    if integer >= 0:
        return bin(integer)[2:].zfill(digits)
    else:
        return bin(2**digits + integer)[2:]

This produces:

>>> int2bin(10, 8)
'00001010'
>>> int2bin(-10, 8)
'11110110'
>>> int2bin(-128, 8)
'10000000'
>>> int2bin(127, 8)
'01111111'
Daniel
  • 3,188
  • 14
  • 34
DeanM
  • 4,695
  • 1
  • 10
  • 11
8

you can do like that :

bin(10)[2:]

or :

f = str(bin(10))
c = []
c.append("".join(map(int, f[2:])))
print c
Skiller Dz
  • 897
  • 10
  • 17
7

Using numpy pack/unpackbits, they are your best friends.

Examples
--------
>>> a = np.array([[2], [7], [23]], dtype=np.uint8)
>>> a
array([[ 2],
       [ 7],
       [23]], dtype=uint8)
>>> b = np.unpackbits(a, axis=1)
>>> b
array([[0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8)
pitfall
  • 2,531
  • 1
  • 21
  • 21
  • The question is about a **string** representation. Still, this happened to be just what I was looking for without going via string first! :) – Tom Hale Apr 30 '19 at 09:30
  • 1
    The [doco](https://docs.scipy.org/doc/numpy/reference/generated/numpy.unpackbits.html#numpy-unpackbits) says: Unpacks elements of a **`uint8`** array into a binary-valued output array. So good for values up to 255. – Tom Hale Apr 30 '19 at 09:32
5

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 string

A faster version without reversing the string.

def int2bin(val):
   res=''
   while val>0:
       res = chr((val&1) + 0x30) + res
       val=val>>1    
   return res 
Reza Abtin
  • 205
  • 3
  • 7
  • The second version is definitely not faster as you end up with something like an O(N^2) algorithm instead of a O(N). I've seen things like this kill an application (performance-wise) because the developer thought doing an extra pass at the end was slower than doing some extra stuff in the first loop. Once fixed brought the running time down from days to seconds. – Andreas Magnusson Apr 15 '19 at 12:21
5

numpy.binary_repr(num, width=None)

Examples from the documentation link above:

>>> np.binary_repr(3)
'11'
>>> np.binary_repr(-3)
'-11'
>>> np.binary_repr(3, width=4)
'0011'

The two’s complement is returned when the input number is negative and width is specified:

>>> np.binary_repr(-3, width=3)
'101'
>>> np.binary_repr(-3, width=5)
'11101'
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
5

The accepted answer didn't address negative numbers, which I'll cover. In addition to the answers above, you can also just use the bin and hex functions. And in the opposite direction, use binary notation:

>>> bin(37)
'0b100101'
>>> 0b100101
37

But with negative numbers, things get a bit more complicated. The question doesn't specify how you want to handle negative numbers.

Python just adds a negative sign so the result for -37 would be this:

>>> bin(-37)
'-0b100101'

In computer/hardware binary data, negative signs don't exist. All we have is 1's and 0's. So if you're reading or producing binary streams of data to be processed by other software/hardware, you need to first know the notation being used.

One notation is sign-magnitude notation, where the first bit represents the negative sign, and the rest is the actual value. In that case, -37 would be 0b1100101 and 37 would be 0b0100101. This looks like what python produces, but just add a 0 or 1 in front for positive / negative numbers.

More common is Two's complement notation, which seems more complicated and the result is very different from python's string formatting. You can read the details in the link, but with an 8bit signed integer -37 would be 0b11011011 and 37 would be 0b00100101.

Python has no easy way to produce these binary representations. You can use numpy to turn Two's complement binary values into python integers:

>>> import numpy as np
>>> np.int8(0b11011011)
-37
>>> np.uint8(0b11011011)
219
>>> np.uint8(0b00100101)
37
>>> np.int8(0b00100101)
37

But I don't know an easy way to do the opposite with builtin functions. The bitstring package can help though.

>>> from bitstring import BitArray
>>> arr = BitArray(int=-37, length=8)
>>> arr.uint
219
>>> arr.int
-37
>>> arr.bin
'11011011'
>>> BitArray(bin='11011011').int
-37
>>> BitArray(bin='11011011').uint
219
Dolf Andringa
  • 2,010
  • 1
  • 22
  • 36
5

Python 3.6 added a new string formatting approach called formatted string literals or “f-strings”. Example:

name = 'Bob'
number = 42
f"Hello, {name}, your number is {number:>08b}"

Output will be 'Hello, Bob, your number is 00001010!'

A discussion of this question can be found here - Here

Tim Uzlov
  • 181
  • 2
  • 6
4
def binary(decimal) :
    otherBase = ""
    while decimal != 0 :
        otherBase  =  str(decimal % 2) + otherBase
        decimal    //=  2
    return otherBase

print binary(10)

output:

1010

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
mukundan
  • 99
  • 1
  • 1
4
n=input()
print(bin(n).replace("0b", ""))
Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
dblaze
  • 41
  • 1
4

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

Van Gale
  • 43,536
  • 9
  • 71
  • 81
3

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):
      remainder = number%2
      number = number/2
      result += str(remainder)
  return result[::-1] # to invert the string
quents
  • 111
  • 3
  • 8
3

Calculator with all neccessary functions for DEC,BIN,HEX: (made and tested with Python 3.5)

You can change the input test numbers and get the converted ones.

# CONVERTER: DEC / BIN / HEX

def dec2bin(d):
    # dec -> bin
    b = bin(d)
    return b

def dec2hex(d):
    # dec -> hex
    h = hex(d)
    return h

def bin2dec(b):
    # bin -> dec
    bin_numb="{0:b}".format(b)
    d = eval(bin_numb)
    return d,bin_numb

def bin2hex(b):
    # bin -> hex
    h = hex(b)
    return h

def hex2dec(h):
    # hex -> dec
    d = int(h)
    return d

def hex2bin(h):
    # hex -> bin
    b = bin(h)
    return b


## TESTING NUMBERS
numb_dec = 99
numb_bin = 0b0111 
numb_hex = 0xFF


## CALCULATIONS
res_dec2bin = dec2bin(numb_dec)
res_dec2hex = dec2hex(numb_dec)

res_bin2dec,bin_numb = bin2dec(numb_bin)
res_bin2hex = bin2hex(numb_bin)

res_hex2dec = hex2dec(numb_hex)
res_hex2bin = hex2bin(numb_hex)



## PRINTING
print('------- DECIMAL to BIN / HEX -------\n')
print('decimal:',numb_dec,'\nbin:    ',res_dec2bin,'\nhex:    ',res_dec2hex,'\n')

print('------- BINARY to DEC / HEX -------\n')
print('binary: ',bin_numb,'\ndec:    ',numb_bin,'\nhex:    ',res_bin2hex,'\n')

print('----- HEXADECIMAL to BIN / HEX -----\n')
print('hexadec:',hex(numb_hex),'\nbin:    ',res_hex2bin,'\ndec:    ',res_hex2dec,'\n')
HKC72
  • 502
  • 1
  • 8
  • 22
2

Somewhat similar solution

def 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 = bin_str[::-1] # reverse the string
    return bin_str 
Chandler
  • 21
  • 1
2

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 = divmod(number, 2)
        bin = bin + str(rem)
    return bin
user210021
  • 59
  • 2
2

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 range(10)])

['', '1', '10', '11', '100', '101', '110', '111', '1000', '1001']
ergonaut
  • 6,929
  • 1
  • 17
  • 47
2

To calculate binary of numbers:

print("Binary is {0:>08b}".format(16))

To calculate the Hexa decimal of a number:

print("Hexa Decimal is {0:>0x}".format(15))

To Calculate all the binary no till 16::

for i in range(17):
   print("{0:>2}: binary is {0:>08b}".format(i))

To calculate Hexa decimal no till 17

 for i in range(17):
    print("{0:>2}: Hexa Decimal is {0:>0x}".format(i))
##as 2 digit is enogh for hexa decimal representation of a number
1
try:
    while True:
        p = ""
        a = input()
        while a != 0:
            l = a % 2
            b = a - l
            a = b / 2
            p = str(l) + p
        print(p)
except:
    print ("write 1 number")
Tom Wyllie
  • 2,020
  • 13
  • 16
Advay168
  • 607
  • 6
  • 10
1

I found a method using matrix operation to convert decimal to binary.

import numpy as np
E_mat = np.tile(E,[1,M])
M_order = pow(2,(M-1-np.array(range(M)))).T
bindata = np.remainder(np.floor(E_mat /M_order).astype(np.int),2)

Eis input decimal data,M is the binary orders. bindata is output binary data, which is in a format of 1 by M binary matrix.

Galle He
  • 11
  • 4
1

I feel Martijn Pieter's comment deserves to be highlighted as an answer:

binary_string = format(value, '0{}b'.format(width))

To me is is both clear and versatile.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
0

If you are willing to give up "pure" Python but gain a lot of firepower, there is Sage - example here:

sage: a = 15
sage: a.binary()
'1111'

You'll note that it returns as a string, so to use it as a number you'd want to do something like

sage: eval('0b'+b)
15
kcrisman
  • 4,374
  • 20
  • 41
0

Here's a simple binary to decimal converter that continuously loops

t = 1
while t > 0:
    binaryNumber = input("Enter a binary No.")
    convertedNumber = int(binaryNumber, 2)

    print(convertedNumber)

print("")
  • This is the reverse order of what the OP wants. They are looking for int to binary. You provide binary to int. – Cecilia Mar 10 '17 at 16:55
0

This is my answer it works well..!

def binary(value) :
    binary_value = ''
    while value !=1  :
        binary_value += str(value%2)
        value = value//2
    return '1'+binary_value[::-1]
-1

Along a similar line to Yusuf Yazici's answer

def 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 result[::-1]

I adjusted it so that the only variable being mutated is result (and n of course).

If you need to use this function elsewhere (i.e., have the result used by another module), consider the following adjustment:

def intToBin(n):
    if(n < 0):
        return -1
    elif(n == 0):
        return str(n)
    else:
        result = ""
        while(n != 0):
            result += str(n%2)
            n //= 2            #added integer division
        return result[::-1]

So -1 will be your sentinel value indicating the conversion failed. (This is assuming you are converting ONLY positive numbers, whether they be integers or longs).

JFMoya
  • 93
  • 6
-1

Here is a (debugged) program that uses divmod to construct a binary list:

Program

while True:
    indecimal_str = input('Enter positive(decimal) integer: ')
    if indecimal_str == '':
        raise SystemExit
    indecimal_save = int(indecimal_str)
    if indecimal_save < 1:
        print('Rejecting input, try again')
        print()
        continue
    indecimal = int(indecimal_str)
    exbin = []
    print(indecimal, '<->', exbin)
    while True:
        if indecimal == 0:
            print('Conversion:', indecimal_save, '=', "".join(exbin))
            print()
            break
        indecimal, r = divmod(indecimal, 2)
        if r == 0:
            exbin.insert(0, '0')
        else:
            exbin.insert(0, '1')
        print(indecimal, '<->', exbin)

Output

Enter positive(decimal) integer: 8
8 <-> []
4 <-> ['0']
2 <-> ['0', '0']
1 <-> ['0', '0', '0']
0 <-> ['1', '0', '0', '0']
Conversion: 8 = 1000

Enter positive(decimal) integer: 63
63 <-> []
31 <-> ['1']
15 <-> ['1', '1']
7 <-> ['1', '1', '1']
3 <-> ['1', '1', '1', '1']
1 <-> ['1', '1', '1', '1', '1']
0 <-> ['1', '1', '1', '1', '1', '1']
Conversion: 63 = 111111

Enter positive(decimal) integer: 409
409 <-> []
204 <-> ['1']
102 <-> ['0', '1']
51 <-> ['0', '0', '1']
25 <-> ['1', '0', '0', '1']
12 <-> ['1', '1', '0', '0', '1']
6 <-> ['0', '1', '1', '0', '0', '1']
3 <-> ['0', '0', '1', '1', '0', '0', '1']
1 <-> ['1', '0', '0', '1', '1', '0', '0', '1']
0 <-> ['1', '1', '0', '0', '1', '1', '0', '0', '1']
Conversion: 409 = 110011001
CopyPasteIt
  • 532
  • 1
  • 8
  • 22