Python Concepts/Bytes Objects And Bytearrays - Wikiversity
bytes object displayed
[edit | edit source]A bytes object is displayed as a sequence of bytes between quotes and preceded by 'b' or 'B':
>>> bytes(3) # This initialization produces an empty sequence of 3 bytes. b'\x00\x00\x00' >>> >>> bytes([3]) # Initialized with a list containing 1 member. b'\x03' >>> >>> B'\x00\x00\x00' b'\x00\x00\x00' >>> >>> isinstance(b'\x00\x00\x00', bytes) True >>> len(b'\x00\x00\x00') 3 # bytes >>>The representation '\x00' is not read literally. This representation means a byte with value 0x00.
If a member of a bytes object can be displayed as a printable ASCII character, then it is so displayed.
>>> B"123ABC" b'123ABC' >>> >>> B'''\x03 1 2 x y \xE7''' b'\x03 1 2 x y \xe7' >>> >>> b"""\x41\x42\x43""" b'ABC' >>>When you look at the contents of a bytes object, it is easy to overlook embedded ASCII characters:
>>> B'\x05\x97\xa3q\xf9\x17\x83' == b'\x05\x97\xa3' + b'q' + B'\xf9\x17\x83' True >>>Parts of a bytes object:
>>> b'0123456789'[5] 53 # Individual member is returned as int. >>> chr(53) '5' >>> >>> b'0123456789'[5:8] b'567' # Sequence of members is returned as slice. >>> >>> b'0123456789'[5:6] b'5' # A slice containing 1 byte. >>> >>> b'0123456789'[2::3] # A slice containing the byte in position 2, then every 3rd until end. b'258' >>> b'0123456789'[2::3] == b'0123456789'[2:3] + b'0123456789'[5:6] + b'0123456789'[8:9] True >>> >>> b1 = b'0123456789' >>> b1[7:3:-1] b'7654' # slice of b1 reversed >>> b1[-1: -len(b1)-1: -1] b'9876543210' # b1 reversed >>> >>> ( b1[-1: -len(b1)-1: -1] ... == b1[-1:: -1] ... == b1[len(b1)-1:: -1] ... == b1[:: -1] # A simple way to reverse the bytes object. ... == b1[len(b1)-1: -len(b1)-1: -1] ) True # Slicing works as expected. >>>Some control characters are recognized as such but not displayed as such:
>>> b' \v\f\n\t ' b' \x0b\x0c\n\t ' >>>bytes object initialized
[edit | edit source] >>> B'\x20\x19\x61\x62\x39\x40' # hex values b' \x19ab9@' >>> >>> b'\101\102\103\104' # octal values b'ABCD' >>> >>> b'The quick, brown fox ...' # ASCII values b'The quick, brown fox ...' >>> >>> b'\x54\x68\x65\161\165\151\143\153, brown \x2E\x2e\056\056' # Mixture. b'The quick, brown ....' >>> >>> b'\056\056\366\367' b'..\xf6\xf7' # If ASCII cannot be displayed, hex is displayed. >>>The bytes object can contain recognized control characters:
>>> a = b'''line1 ... line2 ... line3 ''' >>> a b'line1\n line2 \n line3 \t\t\t' >>>As for strings prefix 'r' or 'R' may be used:
>>> b1 = B'\x41\x42' ; b1 ; len(b1) b'AB' 2 >>> b2 = rB'\x41\x42' ; b2 ; len(b2) b'\\x41\\x42' 8 >>> >>> b2 == b'\x5C' + b'x41' + B"""\134""" + b'x42' True >>>A suitable sequence can be converted to bytes:
>>> b1 = bytes([16, 0x20, ord('a'), 98, 0x63, 0o144, ord(' '), 0xEe]) # Input to bytes() is list. >>> b1 b'\x10 abcd \xee' >>> >>> b2 = bytes( tuple(b1) ) ; b2 # Input to bytes() is tuple. b'\x10 abcd \xee' >>> b2 == b1 True >>> b2 is b1 False # A deep copy. >>> >>> b2 = bytes( [ ord(p) for p in '123 abc DEF' ] ) ; b2 # Converting string to bytes. b'123 abc DEF' >>> >>> bytes( [ ord(p) for p in 'xyz £ § « ® ½ Ø ñ' ] ) b'xyz \xa3\xa7\xab\xae\xbd\xd8\xf1' # Characters chr(0x80) through chr(0xFF) are not displayable ASCII. >>> >>> bytes([0x123]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: bytes must be in range(0, 256) >>>Like a string the bytes object doesn't support item assignment:
>>> a b'line1\n line2 \n line3 \t\t\t' >>> a[3]=6 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bytes' object does not support item assignment >>>Like a string the bytes object can be repeated:
>>> B"1,2," * 3 b'1,2,1,2,1,2,' >>>Behavior of str and behavior of bytes can be significantly different:
>>> c1 = '\u0041' ; c1 ; len(c1) 'A' 1 >>> b1 = b'\u0041' ; b1 ; len(b1) b'\\u0041' 6 >>>The concatenation of 2 or more bytes objects:
>>> b'abc' + B''' ''' + b"""\x44\x45\x46""" + b"\040" + B'\06123' b'abc DEF 123' >>>bytes object as iterable
[edit | edit source]The bytes object accepts the usual operations over iterables:
>>> b1 = b'\t\n abcd' >>> >>> if 'a' in b1 : print ('found') ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a bytes-like object is required, not 'str' >>> if b'a' in b1 : print ('found') ... found >>> >>> if b'\n ab' in b1 : print ('found') ... found >>> ord('c') 99 >>> if 99 in b1 : print ('found') ... found >>> >>> for p in b1 : print (p) ... 9 # p is returned and displayed as int. 10 32 97 98 99 100 >>>Từ khóa » B' X00 X10'
-
Convert B'\x10' To Int 16 In Python - Stack Overflow
-
Python Bytes, Bytearray - W3resource
-
Parsing Question - Python Forum
-
How To Convert Bytes To Int In Python? - GeeksforGeeks
-
Python Code Snippet - ReqBin
-
Go Playground - The Go Programming Language
-
How To Read A File In Python - Able
-
Speakeasy/ At Master - Windows - GitHub
-
Master - GitHub
-
-
Problem With Bytearray Conversion - MicroPython Forum
-
How Can I Use One Drive File Content Into My Code Without Saving Into ...