import struct, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
DUMP = r"C:/Users/vikto/Documents/Claude/rt-4d/radio-spi-dump.bin"
data = open(DUMP, "rb").read()
def hx(b): return " ".join(f"{x:02X}" for x in b)
def bcd(bb):
    if all(b==0xFF for b in bb): return 0
    r=0
    for bv in reversed(bb):
        hi=(bv>>4)&0xF; lo=bv&0xF
        if hi==0xF:hi=0
        if lo==0xF:lo=0
        r=r*100+hi*10+lo
    return r
CT=0x05E000
STRIDE=21
print("21-byte contact records @0x5E000:")
for i in range(4):
    off=CT+i*STRIDE
    d=data[off:off+STRIDE]
    typ=d[0]
    idraw=d[1:5]
    name=bytes(b for b in d[5:21] if b!=0xFF and b!=0).decode('gbk','ignore')
    print(f"rec{i} @0x{off:06X} type={typ:02X} idbytes={hx(idraw)} bcd={bcd(idraw)} le={struct.unpack('<I',idraw)[0]} name={name!r}")
    print("   raw:", hx(d))
# Confirm All Call id: AA AA AA AA. In DMR all-call id=0xFFFFFF(16777215). AA is not FF...
# maybe type: 0=private,1=group,2=allcall. All Call rec type=02 ✓, TG6/TG666 type=01=group ✓
