I need to convert a byte array to a string to send to an SPI device.
Is there a more efficient way of doing this ?
def writebytes(bytes):
str = ""
for i in bytes: str += chr(i)
self.spi.transfer(str)
Use "".join with a generator expression.
def writebytes(bytes):
self.spi.transfer("".join(chr(i) for i in bytes))
"".join(map(char, bytes)) because when a generator expression is passed, join has to iterate over the results to get the total length of input and save them for later. When passing a list, the elements will be available anytime.