def redundantCuantity(dataBits): cuantity = 0 for i, j in dataBits: i = DB.baseToDecimal(i, __BIN_BASE__) if str(i) == str(lesserPowerOfTwo(i)): cuantity += 1 print(cuantity) return cuantity
def numberToExcess(number): number = DB.baseToDecimal(number, __BIN_BASE__) number = list(number) number = [str(int(i) + 3) for i in number] number = "".join(number) # Retorno como BCD number = numberToBcd(DB.decimalToBase(number, __BIN_BASE__)) return number
def numberToBcd(number): # number en BIN number = DB.baseToDecimal(number, __BIN_BASE__) number = list(number) number = [DB.decimalToBase(i, __BIN_BASE__) for i in number] # This block of code gives us the binary BCD for i in range(len(number)): while len(number[i]) < 4: number[i] = '0' + number[i] number = "".join(number) number = str(int(number)) return number
def hammmingToNumber(number, base): #number is BIN c = 0 power = 2**c number = list(number) while power < len(number): number[power - 1] = "" c += 1 power = 2**c number = "".join(number) number = DB.baseToDecimal(number, __BIN_BASE__) number = DB.decimalToBase(number, base) return number
def grayToNumber(number, base): # number == BIN posicion number = list(number) for i in range(1, len(number)): if int(number[i]) + int(number[i - 1]) == 1: number[i] = "1" else: number[i] = "0" number = "".join(number) # number esta en BIN aqui number = DB.baseToDecimal(number, __BIN_BASE__) number = DB.decimalToBase(number, base) return number
def numberToJohnson(number): # number en BIN number = DB.baseToDecimal(number, __BIN_BASE__) bits = lesserPowerOfTwo(number) returnList = list() for i in range(bits): returnList.append("0") index = bits - 1 for i in range(int(number)): if index == __START_POS__ - 1 and returnList[__START_POS__] == "1": index = bits - 1 if returnList[index] == "0": returnList[index] = "1" else: returnList[index] = "0" index -= 1 returnList = "".join(returnList) return returnList
def numberToHamming(number): error = -1 while error != 0: number = list(number) maxLenBin = len(DB.decimalToBase(len(number), __BIN_BASE__)) number = list(enumerate(number)) number = [[decimalToBinWithCeros(i + 1, maxLenBin), j] for i, j in number] redundantC = redundantCuantity(number) pos = maxLenBin - 1 error = list() for i in range(redundantC): bit = "" c = 0 for i, j in number: if i[pos] == "1": if j == "1": c += 1 bit = c * "1" if (int(numberToParity(bit))): error.append("0") else: error.append("1") pos -= 1 error.reverse() error = "".join(error) error = DB.baseToDecimal(error, __BIN_BASE__) error = int(error) errorValue = number[error - 1][1] if error > 0: if (int(errorValue)): number[error - 1][1] = '0' else: number[error - 1][1] = '1' number = [j for i, j in number] number = "".join(number) return number
def bcdBinToNumber(binary): nibbles = int(len(binary) / 4) decimalValue = "" maxIndex = 3 + 1 minIndex = 0 # Iteration for each 4 bit segment for i in range(nibbles): binaryValue = "" # Indexing error resolver #if manIndex > 0: #minIndex = 0 # We get the nibble segment value for i in range(minIndex, maxIndex): binaryValue += binary[i] decimalValue += DB.baseToDecimal(binaryValue, __BIN_BASE__) minIndex += 4 maxIndex += 4 return decimalValue
def pentabitToNumber(number, base): # number en BIN number = DB.baseToDecimal(number, __BIN_BASE__) number = DB.decimalToBase(number, base) return number