Python 3 Building an array of bytes
Solution 1
Use a bytearray
:
>>> frame = bytearray()
>>> frame.append(0xA2)
>>> frame.append(0x01)
>>> frame.append(0x02)
>>> frame.append(0x03)
>>> frame.append(0x04)
>>> frame
bytearray(b'\xa2\x01\x02\x03\x04')
or, using your code but fixing the errors:
frame = b""
frame += b'\xA2'
frame += b'\x01'
frame += b'\x02'
frame += b'\x03'
frame += b'\x04'
Solution 2
what about simply constructing your frame from a standard list ?
frame = bytes([0xA2,0x01,0x02,0x03,0x04])
the bytes()
constructor can build a byte frame from an iterable containing int
values. an iterable is anything which implements the iterator protocol: an list, an iterator, an iterable object like what is returned by range()
...
Solution 3
frame = b'\xa2\x01\x02\x03\x04'
wasn't mentionned till now...
Solution 4
agf's bytearray solution is workable, but if you find yourself needing to build up more complicated packets using datatypes other than bytes, you can try struct.pack()
. http://docs.python.org/release/3.1.3/library/struct.html
Related videos on Youtube
Pherrymason
Updated on July 09, 2022Comments
-
Pherrymason almost 2 years
I need to build a tcp frame with raw binary data, but all examples and tutorials I've found talking about bytes always involve conversion from a string, and that's not what I need.
In short, I need to build just an array of bytes:
0xA2 0x01 0x02 0x03 0x04
Please note that I come from C/C++ world.
I've tried this:
frame = b"" frame += bytes( int('0xA2',16) ) frame += bytes( int('0x01',16) ) frame += bytes( int('0x02',16) ) frame += bytes( int('0x03',16) ) frame += bytes( int('0x04',16) )
Then, throw this frame variable to send method of socket, but not working as expected as frame doesn't contain what I want...
I know this is a very basic question about Python, so if you could point me in the right direction...
-
Ethan Furman over 10 yearsThe problem with the above code is that
bytes
behaves very differently if given an integer instead of a list. You should be usingframe += bytes( [ int('0xa2',16) ] )
. -
Robert Siemer about 8 yearsI know that you want(ed) to do this dynamically, but what is completely unclear is this: where do the input bytes come from? You read hex notation from a text file? You write each byte down in the source code? You have them in an bytearray which you got from reading binary from stdin?
-
-
Pherrymason over 12 yearsThank you, but I'm more interested in learning how to do it ;)
-
Venec over 12 yearsWell, scapy is just a nice tool to have for general network and packet projects. I think what youre looking for is something like this: > >>> from struct import * > >>>>>> packet = pack('Bbbbb',162,1,2,3,4,) > >>> packet > '\xa2\x01\x02\x03\x04'
-
Venec over 12 yearsthe second comment is just python, pack the data packet = pack('Bbbbb',162,1,2,3,4,) then you have your frame. Python has no arrays, it only has Lists. for more info refer to the documentation about structs docs.python.org/library/struct.html
-
agf over 12 years@Venec Python 2.6+ does in fact have a
bytearray
type. -
Pherrymason over 12 yearsbut I need to build the frame dynamically
-
Pherrymason over 12 yearsyou added the tag bytearray to the question when the answer may not use it as a solution.
-
agf over 12 years@clinisbut It's still related -- in the general sense, you're using a byte array (the concept) even if you're not using the
bytearray
type. Either of the two methods in my answer should solve your problem. -
Adrien Plisson over 12 yearsthis syntax for building an array of bytes works the same as for a string type: you can use
+
or other similar operators to build your byte array dynamically. -
Adrien Plisson over 12 yearsyou can replace
bytearray
withbytes
and get roughly the same result. abytearray
is a mutable type though, so it may be more useful than abytes
for constructing your frames. -
Lennart Regebro over 12 yearsScapy is brutal overkill for this.
-
Adrien Plisson over 12 years@agf: that's what you do. i was just telling clinisbut the difference between
bytearray
andbytes
. but now i may need to explain the difference between a mutable and an immutable type.