From 818207a4d859a5e040cdfcb99a3fcf3716ac2df9 Mon Sep 17 00:00:00 2001 From: Junzi Sun Date: Thu, 28 Jan 2016 11:14:14 +0100 Subject: [PATCH] simplify hex2bin() --- decoder.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/decoder.py b/decoder.py index f025167..9485106 100644 --- a/decoder.py +++ b/decoder.py @@ -42,11 +42,10 @@ MODES_CHECKSUM_TABLE = [ def hex2bin(hexstr): """Convert a hexdecimal string to binary string, with zero fillings. """ - length = len(hexstr) * 4 - msgbin = bin(int(hexstr, 16))[2:] - while ((len(msgbin)) < length): - msgbin = '0' + msgbin - return msgbin + scale = 16 + num_of_bits = len(hexstr) * math.log(scale, 2) + binstr = bin(int(hexstr, scale))[2:].zfill(int(num_of_bits)) + return binstr def bin2int(binstr): @@ -230,6 +229,7 @@ def cpr2position(cprlat0, cprlat1, cprlon0, cprlon1, t0, t1): return lat, lon + def get_velocity(msg): """Calculate the speed, heading, and vertical rate"""