summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2019-05-29 18:46:58 (GMT)
committerGitHub <noreply@github.com>2019-05-29 18:46:58 (GMT)
commit0c2f9305640f7655ba0cd5f478948b2763b376b3 (patch)
treeeb5b39614be93083e883f7aeb6f3397d8d8b89c2 /Doc/library
parentaacc77fbd77640a8f03638216fa09372cc21673d (diff)
downloadcpython-0c2f9305640f7655ba0cd5f478948b2763b376b3.zip
cpython-0c2f9305640f7655ba0cd5f478948b2763b376b3.tar.gz
cpython-0c2f9305640f7655ba0cd5f478948b2763b376b3.tar.bz2
bpo-22385: Support output separators in hex methods. (#13578)
* bpo-22385: Support output separators in hex methods. Also in binascii.hexlify aka b2a_hex. The underlying implementation behind all hex generation in CPython uses the same pystrhex.c implementation. This adds support to bytes, bytearray, and memoryview objects. The binascii module functions exist rather than being slated for deprecation because they return bytes rather than requiring an intermediate step through a str object. This change was inspired by MicroPython which supports sep in its binascii implementation (and does not yet support the .hex methods). https://bugs.python.org/issue22385
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/binascii.rst22
-rw-r--r--Doc/library/stdtypes.rst18
2 files changed, 38 insertions, 2 deletions
diff --git a/Doc/library/binascii.rst b/Doc/library/binascii.rst
index 89ecddc..98d8679 100644
--- a/Doc/library/binascii.rst
+++ b/Doc/library/binascii.rst
@@ -145,8 +145,8 @@ The :mod:`binascii` module defines the following functions:
platforms, use ``crc32(data) & 0xffffffff``.
-.. function:: b2a_hex(data)
- hexlify(data)
+.. function:: b2a_hex(data[, sep[, bytes_per_sep=1]])
+ hexlify(data[, sep[, bytes_per_sep=1]])
Return the hexadecimal representation of the binary *data*. Every byte of
*data* is converted into the corresponding 2-digit hex representation. The
@@ -155,6 +155,24 @@ The :mod:`binascii` module defines the following functions:
Similar functionality (but returning a text string) is also conveniently
accessible using the :meth:`bytes.hex` method.
+ If *sep* is specified, it must be a single character str or bytes object.
+ It will be inserted in the output after every *bytes_per_sep* input bytes.
+ Separator placement is counted from the right end of the output by default,
+ if you wish to count from the left, supply a negative *bytes_per_sep* value.
+
+ >>> import binascii
+ >>> binascii.b2a_hex(b'\xb9\x01\xef')
+ b'b901ef'
+ >>> binascii.hexlify(b'\xb9\x01\xef', '-')
+ b'b9-01-ef'
+ >>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2)
+ b'b9_01ef'
+ >>> binascii.b2a_hex(b'\xb9\x01\xef', b' ', -2)
+ b'b901 ef'
+
+ .. versionchanged:: 3.8
+ The *sep* and *bytes_per_sep* parameters were added.
+
.. function:: a2b_hex(hexstr)
unhexlify(hexstr)
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 293a1ab..fcb0da7 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -2404,8 +2404,26 @@ data and are closely related to string objects in a variety of other ways.
>>> b'\xf0\xf1\xf2'.hex()
'f0f1f2'
+ If you want to make the hex string easier to read, you can specify a
+ single character separator *sep* parameter to include in the output.
+ By default between each byte. A second optional *bytes_per_sep*
+ parameter controls the spacing. Positive values calculate the
+ separator position from the right, negative values from the left.
+
+ >>> value = b'\xf0\xf1\xf2'
+ >>> value.hex('-')
+ 'f0-f1-f2'
+ >>> value.hex('_', 2)
+ 'f0_f1f2'
+ >>> b'UUDDLRLRAB'.hex(' ', -4)
+ '55554444 4c524c52 4142'
+
.. versionadded:: 3.5
+ .. versionchanged:: 3.8
+ :meth:`bytes.hex` now supports optional *sep* and *bytes_per_sep*
+ parameters to insert separators between bytes in the hex output.
+
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