gdogg371
gdogg371

Reputation: 4122

Converting list of integers into bytes in Python 2.7

I am converting the following set of integers into bytes, for passing to through a socket to a target IP address:

data = bytes([4,1,0,0,0,0, 224 + 53 // 16, 53 % 16])

However, the output I get for this is:

[4, 1, 0, 0, 0, 0, 227, 5] 

This is not what I was expecting. The syntax as above I believe may relate to Python 3 only. Can someone please advise how to amend to get a valid byte output?

Thanks

Upvotes: 1

Views: 848

Answers (1)

pstatix
pstatix

Reputation: 3848

Python 2.7 didnt have a bytes() built-in, it had bytearray():

>>> bytearray([4,1,0,0,0,0, 224 + 53 // 16, 53 % 16])
bytearray(b'\x04\x01\x00\x00\x00\x00\xe3\x05')

Upvotes: 2

Related Questions