Sync C/Python encoding logic