added 6 byte grid coding in 26 bit

added encode and decode maidenhead grid square QTH locator as capital letter into 4 byte (only 26 bit used)
This commit is contained in:
DB1UJ 2022-04-14 08:44:09 +02:00 committed by GitHub
parent 865ee95a77
commit 9542d32c8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -253,3 +253,64 @@ def check_callsign(callsign:bytes, crc_to_check:bytes):
return [True, bytes(call_with_ssid)]
return [False, ""]
def encode_grid(grid):
"""
Args:
grid:string: maidenhead QTH locater [a-r][a-r][0-9][0-9][a-x][a-x]
Returns:
4 bytes contains 26 bit valid data with encoded grid locator
"""
out_code_word = int(0)
grid = grid.upper() # upper case to be save
int_first = ord(grid[0])-65 # -65 offset for 'A' become zero, utf8 table
int_sec = ord(grid[1])-65 # -65 offset for 'A' become zero, utf8 table
int_val = (int_first * 18) + int_sec # encode for modulo devisioni, 2 numbers in 1
out_code_word = (int_val & 0b111111111) # only 9 bit LSB A - R * A - R is needed
out_code_word = out_code_word << 9 # shift 9 bit left having space next bits, letter A-R * A-R
int_val = int(grid[2:4]) # number string to number int, highest value 99
out_code_word = out_code_word | (int_val & 0b1111111) # using bit OR to add new value
out_code_word = out_code_word << 7 # shift 7 bit left having space next bits, letter A-X
int_val = ord(grid[4])-65 # -65 offset for 'A' become zero, utf8 table
out_code_word = out_code_word | (int_val & 0b11111) # using bit OR to add new value
out_code_word = out_code_word << 5 # shift 5 bit left having space next bits, letter A-X
int_val = ord(grid[5])-65 # -65 offset for 'A' become zero, utf8 table
out_code_word = out_code_word | (int_val & 0b11111) # using bit OR to add new value
return out_code_word.to_bytes(length=4, byteorder='big')
def decode_grid(b_code_word:bytes):
"""
Args:
b_code_word:bytes: 4 bytes with 26 bit valid data LSB
Returns:
grid:str: upper case maidenhead QTH locater [A-R][A-R][0-9][0-9][A-X][A-X]
"""
code_word = int.from_bytes(b_code_word, byteorder='big', signed=False)
grid = chr((code_word & 0b11111) + 65)
code_word = code_word >> 5
grid = chr((code_word & 0b11111) + 65) + grid
code_word = code_word >> 7
grid = str(int(code_word & 0b1111111)) + grid
if (code_word & 0b1111111) < 10:
grid = '0' + grid
code_word = code_word >> 9
int_val = int(code_word & 0b111111111)
int_first = int_val // 18
int_sec = int_val % 18
grid = chr(int(int_first)+65) + chr(int(int_sec)+65) + grid
return grid