1- face questions decimal number into a hexadecimal number, the method is not used hex

Question: Given an integer, write an algorithm to convert it to 16 decimal, for negative numbers, you can use two's complement method

DEF tohex (NUM):
     "" " Decimal to Hexadecimal " "" 
    DI = {10: ' A ' ,. 11: ' B ' , 12 is: ' C ' , 13 is: ' D ' , 14: ' E ' , 15: ' F ' } 
    hexStr = "" 
    IF NUM <0:         # input negative 
        NUM = NUM + 2 ** 32 the while NUM> = 16 : 
        REST = 16% NUM     # take the remainder
    
        hexStr = di.get(rest, str(rest)) + hexStr
        num //= 16
    hexStr = di.get(num, str(num)) + hexStr
    return hexStr
   
            

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xnnx/p/interview_question.html