From 9dde1b63d678f3e7e09f3dbf52643789e5e4200d Mon Sep 17 00:00:00 2001 From: Junzi Sun Date: Mon, 4 May 2020 20:30:33 +0200 Subject: [PATCH] fix missing bin2hex() --- pyModeS/c_common.pxd | 3 ++- pyModeS/c_common.pyx | 5 +++++ pyModeS/common.py | 5 +++++ tests/test_c_common.py | 4 +++- tests/test_common.py | 4 +++- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pyModeS/c_common.pxd b/pyModeS/c_common.pxd index fe8892e..de13840 100644 --- a/pyModeS/c_common.pxd +++ b/pyModeS/c_common.pxd @@ -5,7 +5,8 @@ cdef unsigned char int_to_char(unsigned char i) cpdef str hex2bin(str hexstr) cpdef long bin2int(str binstr) -cpdef long hex2int(str binstr) +cpdef long hex2int(str hexstr) +cpdef str bin2hex(str binstr) cpdef unsigned char df(str msg) cpdef long crc(str msg, bint encode=*) diff --git a/pyModeS/c_common.pyx b/pyModeS/c_common.pyx index c92bd70..77dad81 100644 --- a/pyModeS/c_common.pyx +++ b/pyModeS/c_common.pyx @@ -66,6 +66,11 @@ cpdef long hex2int(str hexstr): cumul = 16*cumul + char_to_int(v_hexstr[i]) return cumul +@cython.boundscheck(False) +cpdef str bin2hex(str binstr): + return "{0:X}".format(int(binstr, 2)) + + @cython.boundscheck(False) cpdef unsigned char df(str msg): """Decode Downlink Format vaule, bits 1 to 5.""" diff --git a/pyModeS/common.py b/pyModeS/common.py index cb2df95..d1a4c81 100644 --- a/pyModeS/common.py +++ b/pyModeS/common.py @@ -19,6 +19,11 @@ def bin2int(binstr): return int(binstr, 2) +def bin2hex(binstr): + """Convert a binary string to hexdecimal string.""" + return "{0:X}".format(int(binstr, 2)) + + def df(msg): """Decode Downlink Format value, bits 1 to 5.""" dfbin = hex2bin(msg[:2]) diff --git a/tests/test_c_common.py b/tests/test_c_common.py index 71cc3f8..d55b588 100644 --- a/tests/test_c_common.py +++ b/tests/test_c_common.py @@ -2,7 +2,9 @@ try: from pyModeS.decoder import c_common as common def test_conversions(): - assert common.hex2bin("6E406B") == "011011100100000001101011" + assert common.hex2bin("6E") == "01101110" + assert common.bin2hex("01101110") == "6E" + assert common.bin2hex("1101110") == "6E" def test_crc_decode(): diff --git a/tests/test_common.py b/tests/test_common.py index 6149ca4..e9e871c 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -2,7 +2,9 @@ from pyModeS import common def test_conversions(): - assert common.hex2bin("6E406B") == "011011100100000001101011" + assert common.hex2bin("6E") == "01101110" + assert common.bin2hex("01101110") == "6E" + assert common.bin2hex("1101110") == "6E" def test_crc_decode():