简体   繁体   中英

Transfering Dec into Hex without using hex function in Python

I have to transfer Dec Number into Hex number without using hex() function. Can someone help me? I'm not advanced at all. My teacher told me it's possible to have it in 4 lines. But I can't make it at all. I would appreciate your help. Here is my Python code so far. But sadly it doesn't work.

    x = 1000000000000000000000000000000
    a = input(int())
    for a in range(x):
     if a in range(0, 10):
    print(a)
    elif a in range(10, 16):
      if (a == 10):
        print("A"),
      if (a == 11):
        print("B"),
      if (a == 12):
        print("C"),
      if (a == 13):
        print("D"),
      if (a == 14):
        print("E"),
      if (a == 15):
        print("F"),
    else:
     def dividing16(a):
      while a > 16:
        a = a%16
        if (a == 10):
          print("A"),
        if (a == 11):
          print("B"),
        if (a == 12):
          print("C"),
        if (a == 13):
          print("D"),
        if (a == 14):
          print("E"),
        if (a == 15):
         print("F"),

To convert to hex form you can use this example (4 lines):

number = 65533  # FFFD in hex

m, out = '0123456789ABCDEF', ''
while number:
    out = m[number % 16] + out
    number //= 16

print(out)

Prints:

FFFD

Another method (without using hex() ):

print('{:X}'.format(65533))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM