Python int to binary string?

后端 未结 30 1613
执念已碎
执念已碎 2020-11-22 05:34

Are there any canned Python methods to convert an Integer (or Long) into a binary string in Python?

There are a myriad of dec2bin() functions out on Google... But I

相关标签:
30条回答
  • 2020-11-22 06:00

    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)
    
    0 讨论(0)
  • 2020-11-22 06:00

    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')
    
    0 讨论(0)
  • 2020-11-22 06:00

    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.

    0 讨论(0)
  • 2020-11-22 06:00

    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.

    0 讨论(0)
  • 2020-11-22 06:02
    >>> format(123, 'b')
    '1111011'
    
    0 讨论(0)
  • 2020-11-22 06:02
    n=input()
    print(bin(n).replace("0b", ""))
    
    0 讨论(0)
提交回复
热议问题