@@ -2289,6 +2289,19 @@ the bytes type has an additional class method to read data in that format:
22892289 >>> bytes .fromhex(' 2Ef0 F1f2 ' )
22902290 b'.\xf0\xf1\xf2'
22912291
2292+ A reverse conversion function exists to transform a bytes object into its
2293+ hexadecimal representation.
2294+
2295+ .. method :: bytes.hex()
2296+
2297+ Return a string object containing two hexadecimal digits for each
2298+ byte in the instance.
2299+
2300+ >>> b ' \xf0\xf1\xf2 ' .hex()
2301+ 'f0f1f2'
2302+
2303+ .. versionadded :: 3.5
2304+
22922305Since bytes objects are sequences of integers (akin to a tuple), for a bytes
22932306object *b *, ``b[0] `` will be an integer, while ``b[0:1] `` will be a bytes
22942307object of length 1. (This contrasts with text strings, where both indexing
@@ -2344,6 +2357,19 @@ the bytearray type has an additional class method to read data in that format:
23442357 >>> bytearray .fromhex(' 2Ef0 F1f2 ' )
23452358 bytearray(b'.\xf0\xf1\xf2')
23462359
2360+ A reverse conversion function exists to transform a bytearray object into its
2361+ hexadecimal representation.
2362+
2363+ .. method :: bytearray.hex()
2364+
2365+ Return a string object containing two hexadecimal digits for each
2366+ byte in the instance.
2367+
2368+ >>> bytearray (b ' \xf0\xf1\xf2 ' ).hex()
2369+ 'f0f1f2'
2370+
2371+ .. versionadded :: 3.5
2372+
23472373Since bytearray objects are sequences of integers (akin to a list), for a
23482374bytearray object *b *, ``b[0] `` will be an integer, while ``b[0:1] `` will be
23492375a bytearray object of length 1. (This contrasts with text strings, where
@@ -3458,6 +3484,17 @@ copying.
34583484 supports all format strings, including those that are not in
34593485 :mod: `struct ` module syntax.
34603486
3487+ .. method :: hex()
3488+
3489+ Return a string object containing two hexadecimal digits for each
3490+ byte in the buffer. ::
3491+
3492+ >>> m = memoryview(b"abc")
3493+ >>> m.hex()
3494+ '616263'
3495+
3496+ .. versionadded :: 3.5
3497+
34613498 .. method :: tolist()
34623499
34633500 Return the data in the buffer as a list of elements. ::
0 commit comments