hexathon

Common and uncommon hex string operations for python3
git clone git://git.defalsify.org/python-hexathon.git
Log | Files | Refs | LICENSE

commit 231319cafc193a0af2ab4bf6503f0d63220bb779
parent 621204566fc1e9cf226d2cbb90e17192948898e7
Author: lash <dev@holbrook.no>
Date:   Wed,  4 May 2022 18:05:43 +0000

Handle nopad case in 0x

Diffstat:
Mhexathon/parse.py | 4++++
Mtests/test_basic.py | 11+++++++++++
2 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/hexathon/parse.py b/hexathon/parse.py @@ -38,6 +38,8 @@ def strip_0x(hx, allow_empty=False, compact_value=False, pad=True): v = compact(hx, allow_empty=allow_empty) elif pad: v = even(hx, allow_empty=allow_empty, allow_compact=True) + else: + v = hx return v @@ -51,6 +53,8 @@ def add_0x(hx, allow_empty=False, compact_value=False, pad=True): v = compact(hx, allow_empty=allow_empty) elif pad: v = even(hx, allow_empty=allow_empty, allow_compact=True) + else: + v = hx return '0x' + v diff --git a/tests/test_basic.py b/tests/test_basic.py @@ -23,10 +23,21 @@ class HexTest(unittest.TestCase): self.assertEqual(hexathon.add_0x('abcd'), '0xabcd') self.assertEqual(hexathon.strip_0x('0x000abcd'), '0000abcd') self.assertEqual(hexathon.add_0x('000abcd'), '0x0000abcd') + + + def test_0x_compact(self): self.assertEqual(hexathon.strip_0x('0x000abcd', compact_value=True), 'abcd') self.assertEqual(hexathon.add_0x('000abcd', compact_value=True), '0xabcd') + def test_0x_nopad(self): + v = '0xabcde' + r = hexathon.strip_0x(v) + self.assertEqual(r, '0abcde') + r = hexathon.strip_0x(v, pad=False) + self.assertEqual(r, 'abcde') + + def test_even(self): self.assertEqual(hexathon.even('aBc'), '0aBc')