How do I convert a list of integers into bytes?

MightyPork

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.

MightyPork

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I filter a stream of integers into a list?

How do I convert lists and dictionaries into bytes?

How do I convert an array of integers to binary?

How do I convert a Vector of bytes (u8) to a string

How do I split a list of integers into a list of digit integers?

How do I convert an array of integers to an array of objects?

How do I convert a vector of strings to a vector of integers in a functional way?

How do I convert a string into a string of bytes in python 2.7

How do I convert an ArrayList of integers to a double[]?

(Python) How can I convert a list of integers into a list of sets where each set is a pair of these integers?

In pandas, how do I convert a series of float or none to strings with integers

How do I parse a JSONObject into a list of integers?

How to convert vector of integers to and from bytes?

How do I convert a string into bytes in python?

How do I convert an array of floats into an array of unique integers?

How do I convert an array of bytes to string with Delphi?

How do I convert a string into a vector of bytes in rust?

How do I join a list of integers?

How can I convert a comma delimited string to a list of integers?

How do i convert Swift strings into Integers?

How to convert list of integers to image?

How do I convert a list of ASCII "Bytes" in Python into Hex Bytes

how do I convert a tuple of floats to a tuple of integers

How do I convert a list of integers to a string - Python

How do I convert a list of strings to integers in Python

How do I convert a list of integers into an actual list in python

How do I convert integers in a list to float numbers when the elements contain both words and numbers?

How do I convert a B string to bytes?

How do I convert a matrix of integers to a matrix of lists of integers in numpy?