Python int to binary string?

后端 未结 30 2057
执念已碎
执念已碎 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

    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.

提交回复
热议问题