2

My old code with python 2 has

import array

buf = array.array('c')

In python 3, this chunk of code gives error

E ValueError: bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)

What would be the equivalent in python 3? How to make the code work for both python 2 and 3?

2

1 Answer 1

0

Just check the manual: https://docs.python.org/3/library/array.html

'b' signed char
'B' unsigned char

So you'll probably need B (but we can't be sure - it depends on the rest of your code, I guess). This is also compatible with Python 2.x.

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

1 Comment

Probably worth noting that the C char can be either signed or unsigned (the standard doesn't specify). I imagine c matches whatever the C compiler used to build Python was. Therefore it could match either b or B. It's probably better to specify it anyway.

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.