Base type
The base system can be divided into the following types:
1.1 Binary letter B means
1.2 octal letter O means
1.3 Decimal letter D means
14.4 Hexadecimal letter H means
Binary : Enter one every two, only
the conversion relationship between the number 0 1 and the decimal system is as follows;
- Convert decimal to binary (integer)
(the last one can be written directly)
2. Convert decimal to binary (
if it is a negative number, just add a negative sign in front)
1.3 Binary to decimal integer
1.4 Binary to decimal decimal
octal (0,1,2,3,4,5,6,7)
hexadecimal (0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F)
are the same when converting to decimal between octal and hexadecimal.
Specifically, you can see the following python code
'''用于二进制转换学习的练习代码相关代码和更多笔记教程可在 https://blog.csdn.net/FUTEROX 获取'''
'''多造轮子多学习 '''
def int_to_B(input_):
'''对十进制数进行公式运算处理整数部分'''
flag=True
x=[]
qu_mo = input_
qu_yu = input_
while flag:
qu_yu=qu_mo%2
x.append(qu_yu)
qu_mo=qu_mo//2
if qu_mo==1:
x.append(qu_mo)
flag=False
number_int=''
for i in x[::-1]:
number_int=number_int+str(i)
return number_int
#此时返回的是一个整数部分的二进制数字
#print(number_int)
def float_to_B(f,jindu=4):
'''默认精度为四'''
B=[]#存放整数部分
for i in range(jindu):
f=f*2
b_zhengshu=int(f)
B.append(b_zhengshu)
if b_zhengshu !=0:
f=f-b_zhengshu
float_n=''
for i in B:
float_n=float_n+str(i)
return float_n
if __name__=="__main__":
print("欢迎使用进制转换工具")
print('如果你想退出请按Q(必须为大写)')
flag=True
while flag:
input_ = str(input("请你输入一个十进制的数字;"))
if input_=="Q":
#防止用户误操作必须使用大写字母Q退出
print("感谢你的使用!!!")
flag=False
'''对输入的数字进行判断是否为整数还是小数
如果是小数则对她进行分割,由于英文水平的问题,
变量名多采用中文拼音进行命名'''
if "." in input_:
nub = []
for i in input_:
nub.append(i)
if nub[0] != '0':
way_point = nub.index(".")
int_ = nub[0:way_point]
int_un = ''
for i in int_:
int_un = int_un + str(i)
# print(int_un)
int_input = int(int_un)
int_part = int_to_B(int_input)
float_ = nub[way_point:]
float_part = ''
for i in float_:
float_part = float_part + str(i)
float_part = "0" + float_part
float_part = float(float_part)
float_part = float_to_B(float_part)
# 开始整合字符串,两个函数运算后产生的都是字符串类型
last_number = int_part + '.' + float_part
last_number = float(last_number)
print(last_number)
elif nub[0] == '0':
number = float_to_B(float(input_))
number = '0.' + number
number = float(number)
print(number)
elif "." not in input_ and input_!="Q":
input_ = int(input_)
reason = int(int_to_B(input_))
print(reason)
You can see that the above algorithm converts decimal to binary, so in fact, you only need to replace% 2 and //2 with %8, //8 and so on.
Conversion between different bases
1.1 Binary → Octal System
First, see the corresponding table in the figure below.
Example: The steps to convert binary (11010111.0100111)B to octal are as follows:
-
111 before the decimal point = 7;
-
010 = 2;
-
11 is completed as 011, 011 = 3;
-
010 after the decimal point = 2;
-
011 = 3;
-
1 is completed as 100, 100 = 4;
Of course, you can also convert binary to decimal and then to octal.
1.2 Octal → Binary
Method: Take the one-to-three method, that is, decompose an octal number into three binary numbers, and add the three binary numbers according to the weight to make up the octal number. The decimal point remains the same.
Example: The steps to convert octal (327)O to binary are as follows:
-
3 = 011;
-
2 = 010;
-
7 = 111;
-
Reading, reading from high to low, 011010111, that is (327)O=(11010111)B
1.3 Octal → Hexadecimal
Method: Convert octal to binary, and then convert binary to hexadecimal, with the decimal point unchanged.
Example: The steps to convert octal (327)O to hexadecimal are as follows:
-
3 = 011;
-
2 = 010;
-
7 = 111;
-
0111 = 7;
-
1101 = D;
-
Reading, reading from high to low, D7, that is (327)O=(D7)H.
etc...
Binary addition and subtraction (supplement)