Ram Rachum
Ram Rachum

Reputation: 88678

Getting modulo of bytes-number in Python

When working in Python, let's say I have a long bytes object. I want to get it modulo some number. For example, say I have the bytes object b'hi' and I want it modulo 3, then the result is 26729 % 3 == 2. (The bytes object is seen as one big number.)

How do I do that in Python? (Preferably Python 3.) I'm hoping that there's a relatively elegant way where I don't have to manually compute the number.

Upvotes: 4

Views: 1398

Answers (1)

jfs
jfs

Reputation: 414585

>>> int.from_bytes(b'hi', 'big', signed=False) % 3
2

Upvotes: 5

Related Questions