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 excessToNumber(number, base): # number is BIN bcd number = bcdBinToNumber(number) number = [str(int(i) - 3) for i in number] number = "".join(number) number = DB.decimalToBase(number, base) # Retorna en base del numero original del BCD ( o sea pre-aƱadido el exceso ) return number
def bcdToNumber(number, base): # Remember that checkbase returns true when there's an error # number es bin BCD while len(number) % 4 != 0: number = '0' + number number = bcdBinToNumber(number) number = DB.decimalToBase(number, 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 johnsonToNumber(number, base): # number en BIN numberList = list(number) ceroCount = 0 for value in numberList: if value == "0": ceroCount += 1 if numberList[__START_POS__] == "1": number = len(numberList) + ceroCount else: number = len(numberList) - ceroCount number = DB.decimalToBase(number, base) return number
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 decimalToBinWithCeros(number, maxLen): number = DB.decimalToBase(number, __BIN_BASE__) while len(number) < maxLen: number = '0' + number return number
def pentabitToNumber(number, base): # number en BIN number = DB.baseToDecimal(number, __BIN_BASE__) number = DB.decimalToBase(number, base) return number