fix missing bin2hex()

This commit is contained in:
Junzi Sun 2020-05-04 20:30:33 +02:00
parent 560b737739
commit 9dde1b63d6
5 changed files with 18 additions and 3 deletions

View File

@ -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=*)

View File

@ -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."""

View File

@ -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])

View File

@ -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():

View File

@ -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():