I have some numbers that represent bytes, and I want to make a bytes
object from them, for example b"something"
Here's my attempt, for the record of "making some research". I tried to google it, but found only the other direction (bytes
to list of numbers). My goal is b'\x04\x05\x06'
from 4, 5, 6.
The intelhex
lib is only because that's where I need the bytes
object.
In [1]: from intelhex import IntelHex
In [2]: ih = IntelHex()
In [3]: addr=10
In [4]: a=5
In [5]: b=4
In [6]: c=255
In [7]: ih.puts(addr, bytes(a, b, c))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-def738bb7624> in <module>()
----> 1 ih.puts(addr, bytes(a, b, c))
TypeError: bytes() argument 2 must be str, not int
In [8]: ih.puts(addr, b'{}{}{}'.format(a, b, c))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-eabba8aef324> in <module>()
----> 1 ih.puts(addr, b'{}{}{}'.format(a, b, c))
AttributeError: 'bytes' object has no attribute 'format'
I'm probably doing some stupid mistake here, the bytes()
function should do it.
I found it, stupid mistake indeed
I was missing brackets there.
In [8]: bytes([4,5,6])
Out[8]: b'\x04\x05\x06'
Let it help future googlers.
Collected from the Internet
Please contact javaer1[email protected] to delete if infringement.
Comments