2011-04-26 266 views
21

我在這裏有一個函數可以將十進制轉換爲十六進制,但它會以相反的順序打印它。我將如何解決它?Python將十進制轉換爲十六進制

def ChangeHex(n): 
    if (n < 0): 
     print(0) 
    elif (n<=1): 
     print(n) 
    else: 
     x =(n%16) 
     if (x < 10): 
      print(x), 
     if (x == 10): 
      print("A"), 
     if (x == 11): 
      print("B"), 
     if (x == 12): 
      print("C"), 
     if (x == 13): 
      print("D"), 
     if (x == 14): 
      print("E"), 
     if (x == 15): 
      print ("F"), 
     ChangeHex(n/16) 
+0

參見:[的Python:進制轉換總是兩個數字(http://stackoverflow.com/ q/11676864/562769) – 2017-01-12 20:28:05

回答

13

如果你想編寫的自己,而不是使用內置的功能,這hex(),你可以簡單地做遞歸調用打印當前數字之前:

def ChangeHex(n): 
    if (n < 0): 
     print(0) 
    elif (n<=1): 
     print n, 
    else: 
     ChangeHex(n/16) 
     x =(n%16) 
     if (x < 10): 
      print(x), 
     if (x == 10): 
      print("A"), 
     if (x == 11): 
      print("B"), 
     if (x == 12): 
      print("C"), 
     if (x == 13): 
      print("D"), 
     if (x == 14): 
      print("E"), 
     if (x == 15): 
      print ("F"), 
+0

你真的相信這是高效的編程嗎? ESP。當有一個簡單的十六進制()函數? (令人驚歎!) – Apostolos 2018-01-04 21:28:04

+0

@Apostolos不,我不認爲它是。是什麼讓你這麼想的?然而,我認爲這個問題的原始海報有助於學習如何修正他們的代碼,儘管這不是我推薦在「真實」代碼中使用的解決方案。 – 2018-01-06 07:22:17

+0

好,夠公平的。 – Apostolos 2018-01-07 18:01:54

27

這不正是你問什麼,但你可以在Python中使用的「十六進制」功能:

>>> hex(15) 
'0xf' 
+0

print hex(15); TypeError:'str'對象不可調用 – Babbit 2016-06-28 03:07:22

+0

@Babbit然後你做錯了什麼。您可能命名了變量十六進制或其他東西。 – Zizouz212 2016-10-07 20:42:12

0

而是在打印功能的一切,你可以只允許返回十六進制的值,做世界衛生大會你想要的東西。

def ChangeHex(n): 
    x = (n % 16) 
    c = "" 
    if (x < 10): 
     c = x 
    if (x == 10): 
     c = "A" 
    if (x == 11): 
     c = "B" 
    if (x == 12): 
     c = "C" 
    if (x == 13): 
     c = "D" 
    if (x == 14): 
     c = "E" 
    if (x == 15): 
     c = "F" 

    if (n - x != 0): 
     return ChangeHex(n/16) + str(c) 
    else: 
     return str(c) 

print(ChangeHex(52)) 

有可能是更好的方法來解析十六進制的字母組件,而不是僅僅使用條件。

17

我認爲這個解決方案是優雅:

def toHex(dec): 
    x = (dec % 16) 
    digits = "ABCDEF" 
    rest = dec/16 
    if (rest == 0): 
     return digits[x] 
    return toHex(rest) + digits[x] 

numbers = [0, 11, 16, 32, 33, 41, 45, 678, 574893] 
print [toHex(x) for x in numbers] 
print [hex(x) for x in numbers] 

此輸出:

['0', 'B', '10', '20', '21', '29', '2D', '2A6', '8C5AD'] 
['0x0', '0xb', '0x10', '0x20', '0x21', '0x29', '0x2d', '0x2a6', '0x8c5ad'] 
64

這個怎麼樣:

hex(dec).split('x')[-1] 

例子:

>>> d = 30 
>>> hex(d).split('x')[-1] 
'1e' 

〜Rich

通過在split()的結果中使用-1,即使split返回了1個元素的列表,也可以工作。

+30

+1或更短:'十六進制(十進制)[2:]' – Tadeck 2013-05-29 01:25:37

13

我用

"0x%X" % n 

其中n是十進制數轉換。

-1

非遞歸方法轉換成十進制數爲十六進制

def to_hex(dec): 

    hex_str = '' 
    hex_digits = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F') 
    rem = dec % 16 

    while dec >= rem: 
     remainder = dec % 16 
     quotient = dec/16 
     if quotient == 0: 
      hex_str += hex_digits[remainder] 
     else: 
      hex_str += str(remainder) 
     dec = quotient 

    return hex_str[::-1] # reverse the string 
12

如果沒有'0x'前綴:

'{0:x}'.format(int(dec)) 

內置hex() funtion其他用途。

+0

另請參閱:[Python:十六進制轉換總是兩位數](http://stackoverflow.com/q/11676864/562769) – 2017-01-12 20:28:30

0

要獲得純粹的十六進制值,這可能會有用。它基於Joe的回答:

def gethex(decimal): 
    return hex(decimal)[2:] 
-1

這工作得很好:

def toHex(n): 
    result, remainder = n, [] 
    while result > 0: 
     rem = result%16 
     result = result // 16 
     if rem < 10: 
      remainder.append(str(rem)) 
     else: 
      if rem == 10: 
       remainder.append('A') 
      if rem == 11: 
       remainder.append('B') 
      if rem == 12: 
       remainder.append('C') 
      if rem == 13: 
       remainder.append('D') 
      if rem == 14: 
       remainder.append('E') 
      if rem == 15: 
       remainder.append('F') 
    return ''.join(remainder[::-1]) 
1
def main(): 
    result = int(input("Enter a whole, positive, number to be converted to hexadecimal: ")) 
    hexadecimal = "" 
    while result != 0: 
     remainder = changeDigit(result % 16) 
     hexadecimal = str(remainder) + hexadecimal 
     result = int(result/16) 
    print(hexadecimal) 

def changeDigit(digit): 
    decimal =  [10 , 11 , 12 , 13 , 14 , 15 ] 
    hexadecimal = ["A", "B", "C", "D", "E", "F"] 
    for counter in range(7): 
     if digit == decimal[counter - 1]: 
      digit = hexadecimal[counter - 1] 
    return digit 

main() 

這是我能爲十進制轉換爲十六進制最密集的。 注意:這是在Python版本3.5.1

0

一個版本使用迭代:

def toHex(decimal): 
    hex_str = '' 
    digits = "ABCDEF" 
    if decimal == 0: 
     return '0' 

    while decimal != 0: 
     hex_str += digits[decimal % 16] 
     decimal = decimal // 16 

    return hex_str[::-1] # reverse the string 

numbers = [0, 16, 20, 45, 255, 456, 789, 1024] 
print([toHex(x) for x in numbers]) 
print([hex(x) for x in numbers]) 
0
hex_map = {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'} 

def to_hex(n): 
    result = "" 
    if n == 0: 
     return '0' 
    while n != 0: 
     result += str(hex_map[(n % 16)]) 
     n = n // 16 
    return '0x'+result[::-1] 
相關問題