linlu11 的博客

python怎么写个方程把10进制变成16进制

 

悬赏分:0 - 提问时间2008-10-14 12:28 问题为何被关闭

Given a string representing an integer in base 10 notation, return a string representing the same value in base 16 (hexadecimal) notation. 
Note 1: This function must not use the built-in function hex: the objective is for you to do the conversion yourself, following the algorithm provided. 
Note 2: You may assume that string provided to convert_to_hex is in the format returned by format_to_int. 
上面是原题目
就是要求写个方程叫convert_to_hex(str)
它把输入的东西变成16进制 
 
 
def convert_to_hex(src): 
 dic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
 src=long(src)
 rst=""
 while src>0:
  rst=dic[src%16]+rst
  src=src/16
 return rst 

posted on 2009-12-16 22:17  linlu11  阅读(539)  评论(0编辑  收藏  举报

导航