the.bay.news

Int.to_bytes returning as many bytes as necessary

Discussions on Python.org
Int.to_bytes returning as many bytes as necessary
I have an int. I want it represented as bytes — as many bytes as are needed to represent it. (Yes, the integer is nonnegative and I want an unsigned bytes representation.) So far as I can see the only sensible way to achieve that right now is: b = i.to_bytes( (i.bit_length()+7)//8 ) This feels surprisingly clunky for what I’d expect to be a relatively common operation. Have I overlooked an easier alternative?

0 comments

Sign in to join the discussion — your thebay.events account works here.

No comments yet.