2021-2022-1 20211420《信息安全专业导论》实现进制转化伪代码
参考伪代码
Write "Enter the new base" # 打印进入新进制
Read newBase # 输入新的进制
Write "Enter the number to be converted" # 输入“要被转化的数”文字提示
Read decimalNumber # 输入十进制数
Set quotient to 1 # 将商设置为1
WHILE (quotient is not zero) # 当商不是零
Set quotient to decimalNumber DIV newBase # 将商设置为十进制数DIV新基数
Set remainder to decimalNumber REM newBase # 将余数设置为新基数的十进制数
Make the remainder the next digit to the left in the answer # 将余数作为答案左边的下一个数字
Set decimalNumber to quotient # 将十进制数设置为商
Write "The answer is "
Write answer
十进制转换为二进制或八进制代码
def baseconvert_dectobinoroct():
list_empty = []
a = int(input("请输入一个自然数:"))
b = int(input("请输入转化的进制(输入1至9中的一个进制基数):"))
remainder = a % b
quotient = (a - remainder) / b
while quotient != 0:
list_empty.append(int(remainder))
a = (a - remainder) / b
remainder = a % b
quotient = (a - remainder) / b
else:
list_empty.append(int(remainder))
list_answer = list_empty[::-1]
d = ''
for i in range(len(list_answer)):
c = list_answer[i]
d = str(d) + str(c)
print(d)
baseconvert_dectobinoroct()
十进制转换为十六进制代码
def baseconvert_dectohex():
dict1 = {"0": 0, "1": 1, "2": 2, "3": 3,
"4": 4, "5": 5, "6": 6, "7": 7,
"8": 8, "9": 9, "10": 'A', "11": 'B',
"12": 'C', "13": 'D', "14": 'E', "15": 'F'}
list_empty = []
a = int(input("请输入一个自然数:"))
b = 16
remainder = a % b # 余数
quotient = (a - remainder) / b # 商
while quotient != 0:
list_empty.append(dict1[str(int(remainder))])
a = (a - remainder) / b
remainder = a % b
quotient = (a - remainder) / b
else:
list_empty.append(dict1[str(int(remainder))])
list_answer = list_empty[::-1]
d = ''
for i in range(len(list_answer)):
c = list_answer[i]
d = str(d) + str(c)
print(d)
baseconvert_dectohex()
代码运行