You can probably use the builtin bin function:
bin(8) #'0b1000'
to get the list:
[int(x) for x in bin(8)[2:]]
Although it seems like there's probably a better way...
Answer from mgilson on Stack OverflowYou can probably use the builtin bin function:
bin(8) #'0b1000'
to get the list:
[int(x) for x in bin(8)[2:]]
Although it seems like there's probably a better way...
Try this:
>>> list('{0:0b}'.format(8))
['1', '0', '0', '0']
Edit -- Ooops, you wanted integers:
>>> [int(x) for x in list('{0:0b}'.format(8))]
[1, 0, 0, 0]
Another edit --
mgilson's version is a little bit faster:
$ python -m timeit "[int(x) for x in list('{0:0b}'.format(8))]"
100000 loops, best of 3: 5.37 usec per loop
$ python -m timeit "[int(x) for x in bin(8)[2:]]"
100000 loops, best of 3: 4.26 usec per loop
python - convert integer to binary - Stack Overflow
Converting Integers to binary and placing them in a list.
python - Convert elements of a list into binary - Stack Overflow
performance - How to write a list of integers to a binary file in python - Stack Overflow
How do I convert an integer to binary in Python?
Can Python convert negative integers to binary?
How do I convert binary text back to an integer?
Thank you for visiting.
Close: I solved it myself.
Thank you very much.
import numpy as LAC a = [50,51,52] print(LAC.unpackbits(LAC.array(a , dtype = LAC.uint8).reshape(len(a),1), axis = 1))
For example, when there is a list a = [50,51,52].
I'm looking for a function to convert this to binary numbers all at once.
I already know the algorithm and the coding, but I don't know if there is a function or not.
Therefore, I would like to ask everyone to let me know if the above function exists or not.
Solution
Probably the easiest way is not to use bin() and string slicing, but use features of .format():
'{:b}'.format(some_int)
How it behaves:
>>> print '{:b}'.format(6)
110
>>> print '{:b}'.format(123)
1111011
In case of bin() you just get the same string, but prepended with "0b", so you have to remove it.
Getting list of ints from binary representation
EDIT: Ok, so do not want just a string, but rather a list of integers. You can do it like that:
your_list = map(int, your_string)
Combined solution for edited question
So the whole process would look like this:
your_list = map(int, '{:b}'.format(your_int))
A lot cleaner than using bin() in my opinion.
>>> map(int, bin(6)[2:])
[1, 1, 0]
If you don't want a list of ints (but instead one of strings) you can omit the map component and instead do:
>>> list(bin(6)[2:])
['1', '1', '0']
Relevant documentation:
binlistmap
I am trying to find a way of converting an integer to binary, and then placing that binary into a list. Is there a way to use the bin() command to perform such a task. If not, how should I approach this problem. I am fairly new to python, but have a decent understanding
You can do with list comprehension.
>>> [int(i) for i in bin(8)[2:].zfill(8)]
[0, 0, 0, 0, 1, 0, 0, 0]
bin(8) return the binary representation of an integer 8 and it's always prefixed with 0b. So bin(8)[2:] is to remove these first two characters(ie, 0b). And then you can use .zfill(8) to pad a numeric string with zeros on the left(with the given width 8)
You could build an f-string then iterate over that with map() as follows:
n = 8
lst = list(map(int, f'{n:08b}'))
print(lst)
Output:
[0, 0, 0, 0, 1, 0, 0, 0]
You want to send a single byte for ALPHA if it's < 256, but two bytes if >= 256? This seems weird -- how is the receiver going to know which is the case...???
But, if this IS what you want, then
x = struct.pack(4*'B' + 'HB'[ALPHA<256] + 4*'B', *data)
is one way to achieve this.
If you know the data and ALPHA position beforehand, it would be best to use struct.pack with a big endian short for that position and omit the 0 that might be overwritten:
def output(ALPHA):
data = [2,25,0,ALPHA,0,23,18,188]
format = ">BBBHBBBB"
return struct.pack(format, *data)
output(101) # result: '\x02\x19\x00\x00e\x00\x17\x12\xbc'
output(999) # result: '\x02\x19\x00\x03\xe7\x00\x17\x12\xbc'