[Python-ideas] bitwise operations on bytes

Ron Adam rrr at ronadam.com
Mon Aug 10 19:16:18 CEST 2009

Mark Dickinson wrote:
> On Sat, Aug 8, 2009 at 10:31 PM, Guido van Rossum<guido at python.org> wrote:
>> -- the struct module only handles sizes 2, 4 and 8. I can hack it by
>> going via a hex representation:
>>  i = 10**100
>>  b = bytes.fromhex(hex(i)[2:])
>>  import binascii
>>  j = int(binascii.hexlify(b), 16)
>>  assert j == i
>> but this is a pretty gross hack.
> The first part also doesn't work if hex(i) has odd length.
> [py3k]:
>>>> bytes.fromhex(hex(10**99)[2:])
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: non-hexadecimal number found in fromhex() arg at position 82
> I think the fact that it's non-trivial to get this right first
> time is further evidence that it would be useful to have
> built-in int <-> bytes conversions somewhere.

Are there going to possibly be other conversions to bytes and back? 
(float, string, struct, ...)

It seems to me the type conversion to and from bytes should be on the 
encoded non-byte type, and other types including user created ones could 
follow that pattern. That may allow bytes to work with any type that has 
the required special methods to do the conversions.

Then most of the methods on bytes would be for manipulating bytes in 
various ways.

The constructor for the int type already does base and string conversions, 
extending it to bytes seems like it would be natural.

    int(bytes)            # just like int(string)
    bytes = bytes(int)    # calls int.__to_bytes__() to do the actual work.


More information about the Python-ideas mailing list