Пример #1
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
Пример #2
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