Convert numpy array of integers to 12 bit binary
<p>I neeed to convert a np array of integers to 12 bit binary numbers, in an array format. What would be the best way to go about doing so? </p>

<p>I've been a bit stuck so any help would be appreciated. Thanks!</p>

<p>Here is what I have to convert an integer to binary:</p>

<pre><code>def dec_to_binary(my_int):
"""

Format a number as binary with leading zeros"""
if my_int &lt; 4096:
    x= "{0:12b}".format(my_int)
    return int(x)
else:
    return 111111111111
</code></pre>

<p>Slight correction (replace <code>12b</code> with <code>012b</code>):</p>

<pre><code>def dec_to_binary(my_int):
    """   
    Format a number as binary with leading zeros
    """
    if my_int &lt; 4096:
        return "{0:012b}".format(my_int)
    else:
        return "111111111111"
</code></pre>

<p>Example: </p>

<pre><code>In [10]: n_array = np.array([123,234,234,345, 4097])

In [11]: map(dec_to_binary, n_array)
Out[11]: 
['000001111011',
 '000011101010',
 '000011101010',
 '000101011001',
 '111111111111']
</code></pre>

python arrays python-3.x numpy