summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2015-04-25 23:22:26 (GMT)
committerGregory P. Smith <greg@krypto.org>2015-04-25 23:22:26 (GMT)
commit8cb6569fe14ba8e57ab1a2bea68594747852a9d1 (patch)
tree4391a41ff833b66e6482f5abbf7f0714f23ccf67 /Doc
parent644adc6adaecf5249de68211f70c0825a36fe6f7 (diff)
downloadcpython-8cb6569fe14ba8e57ab1a2bea68594747852a9d1.zip
cpython-8cb6569fe14ba8e57ab1a2bea68594747852a9d1.tar.gz
cpython-8cb6569fe14ba8e57ab1a2bea68594747852a9d1.tar.bz2
Implements issue #9951: Adds a hex() method to bytes, bytearray, & memoryview.
Also updates a few internal implementations of the same thing to use the new built-in code. Contributed by Arnon Yaari.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/stdtypes.rst37
-rw-r--r--Doc/whatsnew/3.5.rst3
2 files changed, 40 insertions, 0 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 26ff309..2c66fca 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -2289,6 +2289,19 @@ the bytes type has an additional class method to read data in that format:
>>> bytes.fromhex('2Ef0 F1f2 ')
b'.\xf0\xf1\xf2'
+A reverse conversion function exists to transform a bytes object into its
+hexadecimal representation.
+
+.. method:: bytes.hex()
+
+ Return a string object containing two hexadecimal digits for each
+ byte in the instance.
+
+ >>> b'\xf0\xf1\xf2'.hex()
+ 'f0f1f2'
+
+ .. versionadded:: 3.5
+
Since bytes objects are sequences of integers (akin to a tuple), for a bytes
object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes
object 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:
>>> bytearray.fromhex('2Ef0 F1f2 ')
bytearray(b'.\xf0\xf1\xf2')
+A reverse conversion function exists to transform a bytearray object into its
+hexadecimal representation.
+
+.. method:: bytearray.hex()
+
+ Return a string object containing two hexadecimal digits for each
+ byte in the instance.
+
+ >>> bytearray(b'\xf0\xf1\xf2').hex()
+ 'f0f1f2'
+
+ .. versionadded:: 3.5
+
Since bytearray objects are sequences of integers (akin to a list), for a
bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be
a bytearray object of length 1. (This contrasts with text strings, where
@@ -3458,6 +3484,17 @@ copying.
supports all format strings, including those that are not in
:mod:`struct` module syntax.
+ .. method:: hex()
+
+ Return a string object containing two hexadecimal digits for each
+ byte in the buffer. ::
+
+ >>> m = memoryview(b"abc")
+ >>> m.hex()
+ '616263'
+
+ .. versionadded:: 3.5
+
.. method:: tolist()
Return the data in the buffer as a list of elements. ::
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index f364317..da7c5bb 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -80,6 +80,9 @@ New built-in features:
* ``bytes % args``, ``bytearray % args``: :pep:`461` - Adding ``%`` formatting
to bytes and bytearray
+* ``b'\xf0\x9f\x90\x8d'.hex()``, ``bytearray(b'\xf0\x9f\x90\x8d').hex()``,
+ ``memoryview(b'\xf0\x9f\x90\x8d').hex()``: :issue:`9951` - A ``hex`` method
+ has been added to bytes, bytearray, and memoryview.
Implementation improvements: