1

I am using Python 3.6 and I need to convert an integer to a list of individual bits. For example, if I have:

def bitstring_to_bytes(s):
    return int(s, 8).to_bytes(8, byteorder='big')

command_binary = format(1, '08b')
bin_to_byte = bitstring_to_bytes(command_binary)

This currently outputs b'\x00\x00\x00\x00\x00\x00\x00\x01'.

But I need to have in a list of integers (but in a hex type format) like so [0x00, 0x00 ... 0x01] in order to pass it to another function. I am stuck at this part.

10
  • 2
    list(bin_to_byte) would give you a list of integers, is that what you want? You won't see [0x00, ...] because that's not how integers are represented in output, although that's a valid literal form. Commented Jun 27, 2017 at 18:37
  • You can try map(int, bin(str(num))[2:]). Commented Jun 27, 2017 at 18:38
  • @jonrsharpe it needs to be in that form since I am going to be sending data over an SPI connection Commented Jun 27, 2017 at 18:50
  • 1
    This appears to be close, sans formatting list(map(hex, tuple(s))) --> ['0x0', '0x0', ..., '0x1'] Commented Jun 27, 2017 at 19:02
  • @dbosk you need to properly specify what you want. As is, the example output you provided is exactly equivalent to the results of list(b'\x00\x00\x00\x00\x00\x00\x00\x01'). So if that isn't what you want, then you need to tell us exactly what that is. Commented Jun 27, 2017 at 19:06

2 Answers 2

1

What about a simple list-comprehension with cast into bytestype?

bin_to_byte = b'\x00\x00\x00\x00\x00\x00\x00\x01'
list_of_bytes = [bytes([i]) for i in bin_to_byte]
print(list_of_bytes)
# [b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x01']

It does almost the same as list(bin_to_byte) expect it will forces to keep bytes rather than int. If you do need a list of int, then yes, list(bin_to_byte) is enough.

As you can see, each items of the list are not int nor str but bytes:

>>> isinstance(list_of_bytes[0], str)
False
>>> isinstance(list_of_bytes[0], int)
False
>>> isinstance(list_of_bytes[0], bytes)
True

Because the problem while using hex is that it will transfrom your items to string even if they have the hexadecimal form e.g

bin_to_byte = b'\x00\x00\x00\x00\x00\x00\x00\x01'
list_of_hex = list(map(hex, (bin_to_byte)))
print(list_of_hex)
# ['0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x1']
print(isinstance(list_of_hex[0], str))
# True
Sign up to request clarification or add additional context in comments.

Comments

0

One liner:

list(map(lambda b: bin(int(b)), list(str(bin( <your integer> ))[2:])))

OR

list(map(lambda b: hex(int(b)), list(str(bin( <your integer> ))[2:])))

It's ugly, but I'm pretty sure it does exactly what you need it to.

3 Comments

The OP has a list of ints as their desired output. These methods provide lists of str. I think the OP is vague about their requirements though.
it sorta works, it gives me a list of strings. Is there any way to make it maintain a hex format? I'll edit the question
Not sure, I'd have to really sit down and test stuff out. I know when you use hex or binary in python i.e. 0x1 or 0b1, it reads it as an int. But if you lose the hex formatting. so if you do [0x1, 0xff, 0x3], python takes it in as [1, 255, 3]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.