What does a '&' stand for in a python bytearray

前端 未结 1 792
北海茫月
北海茫月 2021-01-22 01:21

What does an ampersand & mean at the end of a python bytearray?

e.g.:

x_w = bytearray(b\'\\x00\\x00\\x04\\x12\\xaa\\x12\\x1         


        
相关标签:
1条回答
  • 2021-01-22 01:43

    It's simply the representation of the byte with value 26 (decimal 38), which is the '&' character in ASCII.

    If you print the actual byte values of the bytes literal you used, you can see this clearly:

    >>> print(' '.join('%02x' % b for b in b'\x00\x00\x04\x12\xaa\x12\x12&'))
    00 00 04 12 aa 12 12 26
    

    And the repr of the bytearray object prefers to represent bytes using ASCII characters rather than hex escapes whenever possible. So it will prefer the representation '&' rather than '\x26', even though they are technically equivalent:

    >>> bytearray([0x00, 0x00, 0x04, 0x12, 0xAA, 0x12, 0x12, 0x26])
    bytearray(b'\x00\x00\x04\x12\xaa\x12\x12&')
    
    >>> b'\x26' == b'&'
    True
    
    0 讨论(0)
提交回复
热议问题