diff --git a/tnc/helpers.py b/tnc/helpers.py index 08504ba2..a1dd3ef9 100644 --- a/tnc/helpers.py +++ b/tnc/helpers.py @@ -316,3 +316,49 @@ def decode_grid(b_code_word:bytes): grid = chr(int(int_first)+65) + chr(int(int_sec)+65) + grid return grid + + + +def encode_call(call): + """ + @auther: DB1UJ + Args: + call:string: ham radio call sign [A-Z,0-9], last char SSID 0-63 + + Returns: + 6 bytes contains 6 bits/sign encoded 8 char call sign with binary SSID (only upper letters + numbers, SSID) + """ + out_code_word = int(0) + + call = call.upper() # upper case to be save + + for x in call: + int_val = ord(x)-48 # -48 reduce bits, begin with first number utf8 table + out_code_word = out_code_word << 6 # shift left 6 bit, making space for a new char + out_code_word = out_code_word | (int_val & 0b111111) # bit OR adds the new char, masked with AND 0b111111 + out_code_word = out_code_word >> 6 # clean last char + out_code_word = out_code_word << 6 # make clean space + out_code_word = out_code_word | (ord(call[-1]) & 0b111111) # add the SSID uncoded only 0 - 63 + + return out_code_word.to_bytes(length=6, byteorder='big') + +def decode_call(b_code_word:bytes): + """ + @auther: DB1UJ + Args: + b_code_word:bytes: 6 bytes with 6 bits/sign valid data char signs LSB + + Returns: + call:str: upper case ham radio call sign [A-Z,0-9] + binary SSID + """ + code_word = int.from_bytes(b_code_word, byteorder='big', signed=False) + ssid = chr(code_word & 0b111111) # save the uncoded binary SSID + + call = str() + while code_word != 0: + call = chr((code_word & 0b111111)+48) + call + code_word = code_word >> 6 + call = call[0:-1] + ssid # remove the last char from call and replace with SSID + + return call +