Why do I get “TypeError: ord() expected string of length 1, but int found” using `ord` on binary data in 3.x?

struct.pack produces raw binary data. In Python 3.x, this is represented by the bytes type; in 2.x, bytes is an alias for str.

In Python 3, indexing a bytes object returns the integer value of the byte:

>>> b"abc"[1]
98

Thus, the ord calls are redundant in the original code, and should be removed.

Leave a Comment