1

I've been having a LOT of trouble with this and the other questions don't seem to be what I'm looking for. So basically I have a list of bytes gotten from

bytes = struct.pack('I',4)
bList = list(bytes)
# bList ends up being [0,0,0,4]
# Perform some operation that switches position of bytes in list, etc

So now I want to write this to a file

f = open('/path/to/file','wb')
for i in range(0,len(bList)):
   f.write(bList[i])

But I keep getting the error

TypeError: 'int' does not support the buffer interface

I've also tried writing:

bytes(bList[i]) # Seems to write the incorrect number. 
str(bList[i]).encode()   # Seems to just write the string value instead of byte

1 Answer 1

2

Oh boy, I had to jump through hoops to solve this. So basically I had to instead do

bList = bytes()
bList += struct.pack('I',4)

# Perform whatever byte operations I need to

byteList = []

# I know, there's probably a list comprehension to do this more elegantly
for i in range(0,len(bList)):
    byteList.append(bList[i])

f.write(bytes(byteList))

So bytes can take an array of byte values (even if they're represented in decimal form in the array) and convert it to a proper byteArray by casting

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.