python - Byte conversion fail -


i have problem python's & bitwise operation:

>>> x = 0xc1 >>> y = 0x7f >>> x & y >>> 65 >>> bytes([65]) >>> b'a' 

the problem conversion decimal hex. 65 0x41, python says 'a'. why?

the value have value want. comment:

i using bytes function because want concat result of base64.b64decode(coded_string) 1 more byte @ end.

bytes([65]) creates bytes object single byte numeric value 65. number means depends on how interpret bytes.

the fact repr happens b'a' isn't relevant. value is, 1 byte want. repr of bytes object, the docs explain, uses bytes literal format convenience. byte matches printable ascii character gets represented character, few common values represented backslash escapes \n, , else hex escape, within b'…'

so, repr(bytes([65])) b'a', because byte 65 printable ascii character a.


if want string hexadecimal representation of number 65, can use hex function—or, if want more control on formatting, format function:

>>> hex(65) '0x41' >>> format(65, '02x') '41' 

but that's not want here. want value b'a', , have that.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -