From b0d4f9de981dbf8756391c9b3231d8e437c2f691 Mon Sep 17 00:00:00 2001 From: Keegan Dent Date: Wed, 13 Dec 2023 18:49:38 +0000 Subject: [PATCH 1/2] byteinvert function --- src/bitstruct/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bitstruct/__init__.py b/src/bitstruct/__init__.py index efb4dda..36e94ba 100644 --- a/src/bitstruct/__init__.py +++ b/src/bitstruct/__init__.py @@ -676,6 +676,14 @@ def byteswap(fmt, data, offset=0): return data_swapped.getvalue() +def byteinvert(data): + """Reverse bits in `data` bytes. For example, the bytes ``\\x11\\x22`` + will produce the result ``\\x88\\x44`` + + """ + pack('<' + 'u8' * len(data), *data) + + def compile(fmt, names=None, text_encoding='utf-8', From f3ac5c839f49d627114e98bc9a61b4e4eec3c04e Mon Sep 17 00:00:00 2001 From: Keegan Dent Date: Thu, 18 Apr 2024 01:14:48 +0000 Subject: [PATCH 2/2] byteinvert now bytereverse and tested --- src/bitstruct/__init__.py | 4 ++-- tests/test_bitstruct.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/bitstruct/__init__.py b/src/bitstruct/__init__.py index 36e94ba..a333dd0 100644 --- a/src/bitstruct/__init__.py +++ b/src/bitstruct/__init__.py @@ -676,12 +676,12 @@ def byteswap(fmt, data, offset=0): return data_swapped.getvalue() -def byteinvert(data): +def bytereverse(data): """Reverse bits in `data` bytes. For example, the bytes ``\\x11\\x22`` will produce the result ``\\x88\\x44`` """ - pack('<' + 'u8' * len(data), *data) + return pack('<' + 'u8' * len(data), *data) def compile(fmt, diff --git a/tests/test_bitstruct.py b/tests/test_bitstruct.py index 9504488..34ce6d0 100644 --- a/tests/test_bitstruct.py +++ b/tests/test_bitstruct.py @@ -277,6 +277,15 @@ def test_byteswap(self): unpacked = unpack('u1u5u2u16', byteswap('12', packed)) self.assertEqual(unpacked, (1, 2, 3, 1024)) + def test_bytereverse(self): + """Byte reverse. + + """ + ref = b'\x11\x22\x33\xFF\x00\x80\x7F' + res = b'\x88\x44\xCC\xFF\x00\x01\xFE' + + self.assertEqual(bytereverse(ref), res) + def test_endianness(self): """Test pack/unpack with endianness information in the format string.