2

I'm a beginner at Python and am running this piece of code:

myInteger = 0
multiple = 256
myString = ""

for i in pkt:
    myString += "%02x" %struct.unpack("B", i)[0]
    myInteger += struct.unpack("B", i)[0] * multiple
    multiple = 1

where pkt is a byte string.

This code works fine in Python 2, but after upgrading to 3 it gives an error:

TypeError: 'int' does not support the buffer interace

After some research, I found out that in Python2, a string of bytes is a string and so, when iterating over it we get single byte strings. But in Python3, we get integers and apparently, struct.unpack does not accept integers.

I read the answer for these questions, but I can't understand how to change the code in order to get it running.

Why do I get an int when I index bytes?

iterate over individual bytes in python3

Porting struct.unpack from python 2.7 to 3

How can I iterate over pkt and get a valid value to pass to struct.unpack?

I apologize if this is an obvious question but I really can't seem to get this working. Thanks in advance!

EDIT

So maybe to be more specific, I am trying to do a ble scan using raspberry pi and I am using the code provided by:

https://github.com/switchdoclabs/BeaconAirPython/blob/master/ble/blescan.py

The code runs on Python2, but on Python3 gives the error mentioned above on every call on struct.pack.

====

Sfmar
  • 159
  • 1
  • 13
  • 2
    Why don't you just do `myString += "%02x" % i` and `myInteger += i * multiple` in Python 3 ? Do you have any reason that you have to use `struct.unpack()` ? – ymonad Oct 11 '18 at 10:16
  • 1
    Possible duplicate of [Porting struct.unpack from python 2.7 to 3](https://stackoverflow.com/questions/33958974/porting-struct-unpack-from-python-2-7-to-3) – stovfl Oct 11 '18 at 10:37
  • @stovfl I mentioned that I already read that question, but as I explained I don't know how to apply those answers to my specific code. Hence, my question. – Sfmar Oct 11 '18 at 11:00
  • You want to use the linked GitHub `blescan.py`, which is Python 2 code, with Python 3 without changing anything? – stovfl Oct 11 '18 at 12:13
  • @stovfl I didn't say I didn't want to change anything. Actually, I'm doing quite the opposite, which is finding a solution to run this code in Python3. And the only problem I'm facing is the one I mentioned in my question and I was hoping anyone could help me. – Sfmar Oct 11 '18 at 13:09
  • Tried your code, 2.7 and 3.x got with and without `unpack(...` the same results. Could you provide data example for `pkt`, also best befor `pack(...`, or expected result. – stovfl Oct 11 '18 at 14:06

2 Answers2

1

As stated in the highest ranked answer to Porting struct.unpack from python 2.7 to 3 you can basically replace struct.unpack("B", i)[0] with i.

Hence, the following code in Python 2:

myInteger = 0
multiple = 256
myString = ""

pkt = "python"

for i in pkt:
    myString += "%02x" % struct.unpack("B", i)[0]
    myInteger += struct.unpack("B", i)[0] * multiple
    multiple = 1

print myString, myInteger
# output: 707974686f6e 29234

Becomes in Python 3:

myInteger = 0 
multiple = 256 
myString = "" 

pkt = b"python" 

for i in pkt: 
    myString += "%02x" % i 
    myInteger += i * multiple 
    multiple = 1

print(myString, myInteger)
# output: 707974686f6e 29234
norok2
  • 25,683
  • 4
  • 73
  • 99
  • Tried your answer and indeed the code runs but does not produced the expected results. Maybe I should have been clearer and posting the whole code. Basically, I'm trying to do a BLE scan using raspberry pi and I'm using the code provided by: https://github.com/switchdoclabs/BeaconAirPython/blob/master/ble/blescan.py This code runs on Python2, but gives the error on struct.pack as I mentioned. The pkt variable is obtained via pkt = sock.recv(255). Does this make a difference? Thanks so much! – Sfmar Oct 11 '18 at 10:51
  • You should probably provide the full Python 2 code, including your test cases. – norok2 Oct 11 '18 at 10:54
0

You have to convert the integer to bytes with the int.to_bytes method:

myString += "%02x" %struct.unpack("B", int.to_bytes(i, 1, 'big'))[0]
myInteger += struct.unpack("B", int.to_bytes(i, 1, 'big'))[0] * multiple
blhsing
  • 91,368
  • 6
  • 71
  • 106