Пример #1
0
def convert(operand, inputBase, outputBase):
    #correct edge case of having only a decimal before the num
    if '.' in operand:
        idx = operand.index('.')
        if idx == 0:
            operand = '0' + operand[idx:]
        elif idx == 1 and operand[0] == '-':
            operand = '-0' + operand[1:]

    # input is Decimal
    if inputBase == Base.decimal:
        if outputBase == Base.decimal:
            return operand
        elif outputBase == Base.hexadecimal:
            return decToHex(operand)
        elif outputBase == Base.binary:
            return decToBin(operand)
        elif outputBase == Base.sem:
            return binsem(operand)

    #input is Hex
    elif inputBase == Base.hexadecimal:
        if outputBase == Base.decimal:
            return hexToDec(operand)
        elif outputBase == Base.hexadecimal:
            return operand
        elif outputBase == Base.binary:
            return hexToBin(operand)
        elif outputBase == Base.sem:
            return binsem(hexToDec(operand))

    # input is Binary
    elif inputBase == Base.binary:
        if outputBase == Base.decimal:
            return binToDec(operand)
        elif outputBase == Base.hexadecimal:
            return binToHex(operand)
        elif outputBase == Base.binary:
            return operand
        elif outputBase == Base.sem:
            return binsem(binToDec(operand))

    #input is SEM
    elif inputBase == Base.sem:
        if outputBase == Base.decimal:
            return flsem(operand)
        elif outputBase == Base.hexadecimal:
            return decToHex(normalize(flsem(operand)))
        elif outputBase == Base.binary:
            return decToBin(normalize(flsem(operand)))
        elif outputBase == Base.sem:
            return operand
Пример #2
0
def decToBin( n ):
    isNegative = False
    if n[0] == '-':
        isNegative = True
        n = n[1:]
    n = normalize(n)
    if '.' in n:
        pL = n.find('.')    # location of point
        integer = n[:pL]    
        fraction = n[pL+1:]
        integer = bin(int(integer))[2:] + '.'
        fraction = float(n) - int( float(n) )
        counter = 32
        while counter > 0 and fraction != 0:
            fraction *= 2
            if fraction >= 1:
                integer = integer + str(int(fraction))
                fraction -= int(fraction)
            else:
                integer = integer + '0'
            counter -= 1
        num = removeInsignificantZeroes(integer)
        return ('-' + num) if isNegative else num
    else:
        b = bin(int(n))[2:]
        return ('-' + b) if isNegative else b
Пример #3
0
def decToHex( n ):
    isNegative = False
    if n[0] == '-':
        isNegative = True
        n = n[1:]
    n = normalize(n)
    if '.' in n:
        pL = n.find('.')
        integer = n[:pL]
        fraction = n[pL+1:]
        integer = hex(int(integer))[2:] + '.'
        fraction = float(n) - int(float(n))
        counter = 32
        while counter != 0 and fraction != 0:
            fraction *= 16
            if fraction >= 1:
                if int(fraction) == 10:
                    integer = integer + 'a'
                elif int(fraction) == 11:
                    integer = integer + 'b'
                elif int(fraction) == 12:
                    integer = integer + 'c'
                elif int(fraction) == 13:
                    integer = integer + 'd'
                elif int(fraction) == 14:
                    integer = integer + 'e'
                elif int(fraction) == 15:
                    integer = integer + 'f'
                elif int(fraction) <= 9:
                    integer = integer + str(int(fraction))
                fraction -= int(fraction)
            else:
                integer = integer + '0'
            counter -= 1
        num = removeInsignificantZeroes(integer)
        return ('-'+num) if isNegative else num
    else:
        h = hex(int(n))[2:]
        return ('-' + h) if isNegative else h