def _convert_elevation(bin_data): '''Convert the elevation from 15 bit binary big endian to signed int. Only the last 15 bit are taken from the 2 byte input. @param bin_data: 2 byte binary data.''' chunk_one = bin(ord(bin_data[0]))[2:] chunk_one = '%s%s' % ((8-len(chunk_one))*'0', chunk_one) chunk_one = chunk_one[1:] chunk_two = bin(ord(bin_data[1]))[2:] chunk_two = '%s%s' % ((8-len(chunk_two))*'0', chunk_two) bit_string = '%s%s' % (chunk_one, chunk_two) if bit_string.startswith('1'): # negative, build two-complement result = '' for char in bit_string: result = '%s%s' % (result, '1' if char == '0' else '0') return -(int(result, 2) + 1) else: # positive return int(bit_string, 2)
def _convert_time(data): '''Convert the binary time to a datetime object and return it. @param data: The binary time.''' bin_data = '' for byte in data: bin_chunk = bin(ord(byte))[2:] bin_data = '%s%s%s' % (bin_data, (8-len(bin_chunk))*'0', bin_chunk) year = int(bin_data[0:6], 2) + 2006 month = int(bin_data[6:10], 2) day = int(bin_data[10:15], 2) hour = int(bin_data[15:20], 2) minute = int(bin_data[20:26], 2) second = int(bin_data[26:32], 2) return datetime.datetime(year, month, day, hour, minute, second)
def _convert_speed(bin_data): '''Convert the speed from 9 bit binary big endian to int in km/h. The input has the size of 2 byte, but only the first 9 bit are interpreted. @param bin_data: 2 byte binary data.''' return ord(bin_data[0]) + int(bin(ord(bin_data[1]))[2:3], 2)