Python 十進位制 十六進製制互轉

2021-10-10 22:03:08 字數 2173 閱讀 6908

python-十進位制轉十六進製制

input_str = input("請輸入待轉十進位制數:")

deal_list =

if input_str.isdecimal():

input_int = int(input_str)

while true:

if input_int != 0:

n = input_int % 16

input_int //= 16

elif input_int == 0:

deal_list.reverse()

break

deal_list = [str(k) for k in deal_list]

for j in deal_list:

if j == '10':

t = deal_list.index(j)

deal_list[t] = 'a'

elif j == '11':

t = deal_list.index(j)

deal_list[t] = 'b'

elif j == '12':

t = deal_list.index(j)

deal_list[t] = 'c'

elif j == '13':

t = deal_list.index(j)

deal_list[t] = 'd'

elif j == '14':

t = deal_list.index(j)

deal_list[t] = 'e'

elif j == '15':

t = deal_list.index(j)

deal_list[t] = 'f'

else:

pass

hex_str = ''.join(deal_list)

print("該十進位制數轉化為十六進製制數為:%s" % hex_str)

else:

print("輸入有誤,請重新輸入!")

python-十六進製制轉十進位制

input_str = input("請輸入待轉十六進製制數:")

hex_tuple = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')

hex_int = 0

is_pass = true

frist_deal = input_str.upper()

for s in frist_deal:

if s in hex_tuple:

pass

else:

print("所輸入待轉十六進製制數有誤,請重新輸入!")

is_pass = false

break

if is_pass:

hex_list = [str(j) for j in frist_deal]

for u in hex_list:

if u == 'a':

i = hex_list.index('a')

hex_list[i] = '10'

elif u == 'b':

i = hex_list.index('b')

hex_list[i] = '11'

elif u == 'c':

i = hex_list.index('c')

hex_list[i] = '12'

elif u == 'd':

i = hex_list.index('d')

hex_list[i] = '13'

elif u == 'e':

i = hex_list.index('e')

hex_list[i] = '14'

elif u == 'f':

i = hex_list.index('f')

hex_list[i] = '15'

else:

pass

t = len(hex_list) - 1

for y in hex_list:

hex_int += int(y) * 16 ** t

t -= 1

print("該十六進製制數轉化為十進位制數為:%d" % hex_int)

十進位制 十六進製制

把十進位制整數轉換為十六進製制,格式為0x開頭,10 15由大寫字母a f表示。input 每行乙個整數x,0 x 2 31。output 每行輸出對應的八位十六進製制整數,包括前導0。sample input 0 1023 sample output 0x00000000 0x000003ff 水...

十六進製制轉十進位制

create function fn hextobinary hex varchar 8 returns varchar 255 asbegin declare base tinyint declare string varchar 255 declare return varchar 255 de...

十進位制轉為十六進製制

問題描述 十六進製制數是在程式設計時經常要使用到的一種整數的表示方式。它有0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f共16個符號,分別表示十進位制數的0至15。十六進製制的計數方法是滿16進1,所以十進位制數16在十六進製制中是10,而十進位制的17在十六進製制中是11,以此類推...