import os
def clean_hex_input():
hex_string = input("Enter key here (or type 'exit' to quit): ").replace(" ", "").upper()
if hex_string.lower() == "exit":
return None
if not all(c in "0123456789ABCDEF" for c in hex_string):
print("Invalid input. Ensure it contains only hexadecimal characters.")
return clean_hex_input()
if len(hex_string) == 12:
return hex_string # If exactly 12 characters, skip processing
if len(hex_string) == 16:
return process_hex_string(hex_string) # Process normally
print("Invalid input. It must be either in BISS-1 format (12 characters) or CW format (16 characters).")
return clean_hex_input()
def process_hex_string(hex_string):
return hex_string[:6] + hex_string[8:14] # Removing 7th, 8th, 15th, and 16th characters
def insert_into_file(hex_string, filename="hex_data.txt"):
if not os.path.exists(filename):
print(f"File '{filename}' not found.")
return
with open(filename, "r") as file:
lines = {line.strip() for line in file.readlines()} # Set for fast duplicate checking
if hex_string in lines:
print(f"CW '{hex_string}' already exists in the file.")
return
lines.add(hex_string)
sorted_lines = sorted(lines) # Sorting ensures correct placement
with open(filename, "w") as file:
for line in sorted_lines:
file.write(line + "\n")
print(f"CW '{hex_string}' successfully inserted.")
if __name__ == "__main__":
while True:
raw_hex = clean_hex_input()
if raw_hex is None: # If user types exit
print("Exiting...")
break
insert_into_file(raw_hex)