From 477a0608630d1cc6d7ab674d0096d34ad0742918 Mon Sep 17 00:00:00 2001 From: "Simon A. Eugster" Date: Mon, 25 Sep 2023 15:36:34 +0200 Subject: [PATCH 1/4] Add explanation about little/big endian --- Doc/library/struct.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst index e2e6fc542e3e67..176bd1b6826176 100644 --- a/Doc/library/struct.rst +++ b/Doc/library/struct.rst @@ -160,6 +160,11 @@ following table: If the first character is not one of these, ``'@'`` is assumed. +The number ``1023``, or ``0x3ff`` in hex, is represented as follows in bytes: + +* ``0x03 0xff`` in big-endian (``>``) +* ``0xff 0x03`` in little-endian (``<``) + Native byte order is big-endian or little-endian, depending on the host system. For example, Intel x86, AMD64 (x86-64), and Apple M1 are little-endian; IBM z and many legacy architectures are big-endian. From 770e154f18153c1a376be81efec86ad744c9a59c Mon Sep 17 00:00:00 2001 From: "Simon A. Eugster" Date: Tue, 31 Oct 2023 15:57:57 +0100 Subject: [PATCH 2/4] docs: Apply rephrasing suggestion by AA-Turner Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Doc/library/struct.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst index 176bd1b6826176..ace25a104bb872 100644 --- a/Doc/library/struct.rst +++ b/Doc/library/struct.rst @@ -160,7 +160,7 @@ following table: If the first character is not one of these, ``'@'`` is assumed. -The number ``1023``, or ``0x3ff`` in hex, is represented as follows in bytes: +The number 1023 (``0x3ff`` in hexadecimal) has the following byte representations: * ``0x03 0xff`` in big-endian (``>``) * ``0xff 0x03`` in little-endian (``<``) From 11044b7ceded9872180c0dfc99e6c5eb14872679 Mon Sep 17 00:00:00 2001 From: "Simon A. Eugster" Date: Tue, 31 Oct 2023 16:13:47 +0100 Subject: [PATCH 3/4] docs: Wrap in a note and add Python code example --- Doc/library/struct.rst | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst index ace25a104bb872..f5052755b28197 100644 --- a/Doc/library/struct.rst +++ b/Doc/library/struct.rst @@ -160,10 +160,19 @@ following table: If the first character is not one of these, ``'@'`` is assumed. -The number 1023 (``0x3ff`` in hexadecimal) has the following byte representations: +.. note:: + + The number 1023 (``0x3ff`` in hexadecimal) has the following byte representations: + + * ``03 ff`` in big-endian (``>``) + * ``ff 03`` in little-endian (``<``) + + Python example: -* ``0x03 0xff`` in big-endian (``>``) -* ``0xff 0x03`` in little-endian (``<``) + >>> struct.pack('>h', 1023) + b'\x03\xff' + >>> struct.pack(' Date: Mon, 19 Feb 2024 07:59:05 +0100 Subject: [PATCH 4/4] Add struct import --- Doc/library/struct.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst index f5052755b28197..3e507c1c7e7c85 100644 --- a/Doc/library/struct.rst +++ b/Doc/library/struct.rst @@ -169,6 +169,7 @@ If the first character is not one of these, ``'@'`` is assumed. Python example: + >>> import struct >>> struct.pack('>h', 1023) b'\x03\xff' >>> struct.pack('