6

The following code works fine in python 2.7:

def GetMaxNoise(data, max_noise):
 for byte in data:
 noise = ComputeNoise(struct.unpack('=B',byte)[0])
 if max_noise < noise:
 max_noise = noise
 return max_noise

where data is a string holding binary data (taken from a network packet).

I'm trying to port it to Python 3 and I get this:

File "Desktop/Test.py", line 2374, in GetMaxNoise

noise = ComputeNoise(struct.unpack('=B',byte)[0])

TypeError: 'int' does not support the buffer interface

How can I convert "data" to the appropriate type needed by unpack()?

asked Nov 27, 2015 at 13:58

2 Answers 2

5

Assuming the data variable is a string of bytes that you got from a binary file on a network packet, it it not processed the same in Python2 and Python3.

In Python2, it is a string. When you iterate its values, you get single byte strings, that you convert to int with struct.unpack('=B')[0]

In Python3, it is a bytes object. When you iterate its values, you directly get integers! So you should directly use:

def GetMaxNoise(data, max_noise):
 for byte in data:
 noise = ComputeNoise(byte) # byte is already the int value of the byte...
 if max_noise < noise:
 max_noise = noise
 return max_noise
answered Nov 27, 2015 at 15:40
Sign up to request clarification or add additional context in comments.

1 Comment

In this case it's the better solution, to just cut out the unpack. Yours answer is better :)
1

From the docs of the struct module https://docs.python.org/3.4/library/struct.html I see that the unpack method expects it's second argument to implement Buffer Protocol, so it generally expects bytes.

Your data object seems to be of the type bytes as it's read from somewhere. When you iterate over it with the for loop, you end up with byte variable being single int values.

I don't know what your code is supposed to do and how, but maybe change the way you iterate over your data object to handle not ints but bytes of length == 1?

for i in range(len(data)):
 byte = data[i:i+1]
 print(byte)
answered Nov 27, 2015 at 15:09

1 Comment

Hmm, it works, but you are doing that all only to keep the individual bytes in bytes format to be able to convert them later to integers, when directly iterating a bytes gives the same integer. Looks pretty much like one step forth and the latter back

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.