在十六进制译码器输入端加入0到15的二进制编码(如1011、0101等)显示结果列表记录
“Devil组”引证GPT后的撰写:.
# 定义一个字典,用于将4位二进制编码转换为对应的16进制值
hex_dict = {
'0000': '0',
'0001': '1',
'0010': '2',
'0011': '3',
'0100': '4',
'0101': '5',
'0110': '6',
'0111': '7',
'1000': '8',
'1001': '9',
'1010': 'A',
'1011': 'B',
'1100': 'C',
'1101': 'D',
'1110': 'E',
'1111': 'F'
}
# 读取用户输入的二进制编码,转换为16进制值,并打印到控制台上
while True:
binary_code = input("请输入一个4位二进制编码(例如:1011):")
if binary_code.lower() == 'exit':
break
if len(binary_code) != 4:
print("输入的二进制编码必须是4位!")
continue
if not all(char in '01' for char in binary_code):
print("输入的二进制编码必须只包含0和1!")
continue
hex_value = hex_dict[binary_code]
print("对应的16进制值为:{}".format(hex_value))