进制转换是每个计算机专业的学生都应该熟练掌握的技能,今天在上课的时候有一点小小的感悟,现在在这里分享出来

二进制转换为十进制该怎样快速计算:
比如给我们一个二进制的数(11111111001)2,这样的数难道我们还要向老师说的那样(11111111001)2=(1x210+1x29+1x28…..+1x20)10
这样来计算吗?
答案当然是NO
我们知道(11111111111)2=211-1
我们要计算(11111111001)2那么只需要减去从右往左数第二和第三位的值就可以了
也就是(11111111001)2 = (11111111111)2 - 21 - 22 = 211 - 1 - 21 - 22 = 2041

最后再贴一个自己写的进制转换的python脚本(有点丑),该脚本只实现了二进制,十进制,十六进制的互换,毕竟八进制不实用嘛就懒得写了
输入previous可以返回上级菜单,输入exit退出脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#encoding:utf-8
def main():
print ("请选择你要使用的功能:")
print("1.二进制与十进制转换")
print("2.二进制与十六进制转换")
print("3.十进制与十六进制转换")
command = raw_input("输入你的选择:")
if command == '1':
two_and_ten()
elif command == '2':
two_and_sixteen()
elif command == '3':
ten_and_sixteen()
elif command == "exit":
exit()
else:
print("输入错误,请重新输入!")
main()
def two_and_ten():
print("1.二进制转十进制:")
print("2.十进制转二进制:")
command_2 = raw_input("你的选择:")
if command_2 == '1':
two_to_ten()
elif command_2 == '2':
ten_to_two()
elif command_2 == "exit":
exit()
elif command_2 == "previous":
main()
else:
print("输入错误,请重新输入:")
two_and_ten()
def two_to_ten():
while 1:
number = raw_input("请输入二进制数:")
if number == "exit":
exit()
elif number == "previous":
two_and_ten()
number = "0b" + number
number = int(number, 2)
print "十进制为:",
print number
def ten_to_two():
while 1:
number = raw_input("请输入十进制数:")
if number == "exit":
exit()
elif number == "previous":
two_and_ten()
number = bin(int(number))
print "二进制为:",
print number
def two_and_sixteen():
print("1.二进制转十六进制:")
print("2.十六进制转二进制:")
command_2 = raw_input("你的选择:")
if command_2 == '1':
two_to_sixteen()
elif command_2 == '2':
sixteen_to_two()
elif command_2 == "exit":
exit()
elif command_2 == "previous":
main()
else:
print("输入错误,请重新输入:")
two_and_sixteen()
def two_to_sixteen():
while 1:
number = raw_input("请输入二进制数:")
if number == "exit":
exit()
elif number == "previous":
two_and_sixteen()
number = "0b" + number
number = int(number, 2)
number = hex(number)
print "十六进制为:",
print number
def sixteen_to_two():
while 1:
number = raw_input("请输入十六进制数:")
if number == "exit":
exit()
elif number == "previous":
two_and_sixteen()
number = "0x" + number
number = bin(int(number, 16))
print "二进制为:",
print number
def ten_and_sixteen():
print("1.十进制转十六进制:")
print("2.十六进制转十进制:")
command_2 = raw_input("你的选择:")
if command_2 == '1':
ten_to_sixteen()
elif command_2 == '2':
sixteen_to_ten()
elif command_2 == "exit":
exit()
elif command_2 == "previous":
main()
else:
print("输入错误,请重新输入:")
ten_and_sixteen()
def ten_to_sixteen():
while 1:
number = raw_input("请输入十进制数:")
if number == "exit":
exit()
elif number == "previous":
ten_and_sixteen()
number = hex(int(number))
print "十六进制为:",
print number
def sixteen_to_ten():
while 1:
number = raw_input("请输入十六进制数:")
if number == "exit":
exit()
elif number == "previous":
ten_and_sixteen()
number = "0x" + number
number = int(number, 16)
print "十进制为:",
print number
if __name__ == "__main__":
main()

最后更新: 2018年02月26日 19:58

原始链接: http://drac0nids.top/2018/02/26/py进制转换脚本/

× 请我吃糖~
打赏二维码