diff --git a/pyModeS/decoder/common.py b/pyModeS/decoder/common.py index d11718c..9f69757 100644 --- a/pyModeS/decoder/common.py +++ b/pyModeS/decoder/common.py @@ -15,32 +15,11 @@ def hex2int(hexstr): return int(hexstr, 16) -def int2hex(n): - """Convert a integer to hexadecimal string.""" - # strip 'L' for python 2 - return hex(n)[2:].rjust(6, "0").upper().rstrip("L") - - def bin2int(binstr): """Convert a binary string to integer.""" return int(binstr, 2) -def bin2hex(hexstr): - """Convert a hexdecimal string to integer.""" - return int2hex(bin2int(hexstr)) - - -def bin2np(binstr): - """Convert a binary string to numpy array.""" - return np.array([int(i) for i in binstr]) - - -def np2bin(npbin): - """Convert a binary numpy array to string.""" - return np.array2string(npbin, separator="")[1:-1] - - def df(msg): """Decode Downlink Format vaule, bits 1 to 5.""" dfbin = hex2bin(msg[:2]) @@ -100,7 +79,7 @@ def crc_legacy(msg, encode=False): ) ng = len(generator) - msgnpbin = bin2np(hex2bin(msg)) + msgnpbin = np.array([int(i) for i in hex2bin(msg)]) if encode: msgnpbin[-24:] = [0] * 24 @@ -114,7 +93,9 @@ def crc_legacy(msg, encode=False): msgnpbin[i : i + ng] = np.bitwise_xor(msgnpbin[i : i + ng], generator) # last 24 bits - reminder = bin2int(np2bin(msgnpbin[-24:])) + msgbin = np.array2string(msgnpbin[-24:], separator="")[1:-1] + reminder = bin2int(msgbin) + return reminder @@ -146,7 +127,7 @@ def icao(msg): addr = msg[2:8] elif DF in (0, 4, 5, 16, 20, 21): c0 = crc(msg, encode=True) - c1 = hex2int(msg[-6:]) + c1 = int(msg[-6:], 16) addr = "%06X" % (c0 ^ c1) else: addr = None @@ -159,7 +140,7 @@ def is_icao_assigned(icao): if (icao is None) or (not isinstance(icao, str)) or (len(icao) != 6): return False - icaoint = hex2int(icao) + icaoint = int(icao, 16) if 0x200000 < icaoint < 0x27FFFF: return False # AFI diff --git a/tests/test_common.py b/tests/test_common.py index df2ac36..6149ca4 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -3,8 +3,6 @@ from pyModeS import common def test_conversions(): assert common.hex2bin("6E406B") == "011011100100000001101011" - assert common.bin2hex("011011100100000001101011") == "6E406B" - assert common.int2hex(11160538) == "AA4BDA" def test_crc_decode(): @@ -28,7 +26,7 @@ def test_crc_decode(): def test_crc_encode(): parity = common.crc("8D406B902015A678D4D220AA4BDA", encode=True) - assert common.int2hex(parity) == "AA4BDA" + assert parity == 11160538 def test_icao():