def bitCount(int_type): # return number 1's in a number when represented as binary count = 0 while(int_type): int_type &= int_type - 1 count += 1 return(count) def generate35bitHex(facilityCode, cardCode): # generates a hex code for HID 35 bit format card # see this page to understand formats: #http://www.pagemac.com/azure/data_formats.php cardData = (facilityCode << 21) + (cardCode << 1) # 2nd MSB even parity parity1 = bitCount(cardData & 0x1B6DB6DB6) & 1 cardData += (parity1 << 33) # add the parity bit (we need it for further parity calculations) # MSB odd parity is the LSB parity2 = bitCount(cardData & 0x36DB6DB6C) & 1 ^ 1 cardData += parity2 # add the parity bit # LSB odd parity (covers all 34 other bits) parity3 = bitCount(cardData) & 1 ^ 1 cardData += (parity3 << 34) # add the parity bit return "%09X" % cardData # convert to hex, pad with zeros (9 characters) def generate26bitHex(facilityCode, cardCode): # generates a hex code for HID 26 bit format card # see this page to understand formats: #http://www.pagemac.com/azure/data_formats.php cardData = (facilityCode << 17) + (cardCode << 1) # MSB even parity (covers 12 MSB) parity1 = bitCount(cardData & 0x1FFE000) & 1 # LSB odd parity (covers 12 LSB) parity2 = bitCount(cardData & 0x0001FFE) & 1 ^ 1 cardData += (parity1 << 25) + (parity2) return "%06X" % cardData # convert to hex, pad with zeros (6 characters) facilityCode = 54 cardCode = 360100 generate35bitHex(facilityCode, cardCode)