0828华为刷题:进制转换(含进制知识补充)

01 题目描述

0828华为刷题:进制转换(含进制知识补充)_第1张图片

02 代码如下

while True:
    try:
        x_num = input()[2:]
        d_num = 0
        length = len(x_num)
        for i in range(length):
            if x_num[i] == 'A':
                d_num += 10 * 16 ** (length - 1 - i)
            elif x_num[i] == 'B':
                d_num += 11 * 16 ** (length - 1 - i)
            elif x_num[i] == 'C':
                d_num += 12 * 16 ** (length - 1 - i)
            elif x_num[i] == 'D':
                d_num += 13 * 16 ** (length - 1 - i)
            elif x_num[i] == 'E':
                d_num += 14 * 16 ** (length - 1 - i)
            elif x_num[i] == 'F':
                d_num += 15 * 16 ** (length - 1 - i)
            else:
                d_num += int(x_num[i]) * 16 ** (length - 1 - i)
        print(d_num)
    except:
        break

03 补充:二进制、八进制、十进制与十六进制

  • 二进制:0和1,满2进1
  • 八进制:0-7,满8进1,用0开头表示
    010-101-110
    2 5 6 =八进制数:0256
  • 十进制:0-9,满10进1
  • 十六进制:0-9,A-F,满16进1,用0x开头表示
    1010-1110
    10 14 =十六进制数:0xAE

你可能感兴趣的:(编程学习笔记)