2

Python2 code:

x = buffer(chr(0) * 32)

What is the python3 equivalent? I tried to replace buffer with memoryview() but than name error becomes a type error: TypeError: memoryview: a bytes-like object is required, not 'str'.

I'm pretty sure that this should be a string and not a byte.

Can someone help me?

Buffer function for python 3+ IS NOT THE ANSWER!

python3.py
  • 41
  • 1
  • 4
  • @ThierryLathuille please highlight the similarities of the two questions and how the other one answers this one.. python3.py, please explain the differences to support your claim that there is no solution there for this question. – Yunnosch Oct 21 '20 at 08:06
  • something like `x = memoryview(b'\x00'*32)` ? – Jean-François Fabre Oct 21 '20 at 08:10
  • I said it right there in the question! "I tried to replace buffer with memoryview() but than name error becomes a type error: TypeError: memoryview: a bytes-like object is required, not 'str'." The linked "duplicate" answer nothing! I changed the code to: x = memoryview(b"chr(0) * 32") Which looks pretty bad and I'm sure this is not the right way to d but at least all error is gone. – python3.py Oct 21 '20 at 08:11
  • I did: x = memoryview(b"chr(0) * 32") but I think the solution is the one posted by @Jean-FrançoisFabre – python3.py Oct 21 '20 at 08:14

1 Answers1

0

Adapting answers from Buffer function for python 3+

x = memoryview(b'\x00'*32)`

memoryview expects bytes. Python 3 makes a distinction between bytes and strings now.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219