我硬編碼以下函數將十六進制解碼爲binanry。它看起來不像我想要的那樣優雅,但它的工作原理。有人可以幫助我推廣代碼嗎?十六進制格式轉換和多行打印
def print_hex_to_atp(hex,output_file):
if hex=="0":
output_file.write("> Data 0 end;\n") #print hex 0 in binary
output_file.write("> Data 0 end;\n")
output_file.write("> Data 0 end;\n")
output_file.write("> Data 0 end;\n")
elif hex=="1":
output_file.write("> Data 0 end;\n") #print hex 1 in binary
output_file.write("> Data 0 end;\n")
output_file.write("> Data 0 end;\n")
output_file.write("> Data 1 end;\n")
elif hex=="2":
output_file.write("> Data 0 end;\n") #print hex 2 in binary
output_file.write("> Data 0 end;\n")
output_file.write("> Data 1 end;\n")
output_file.write("> Data 0 end;\n")
elif hex=="3":
output_file.write("> Data 0 end;\n") #print hex 3 in binary
output_file.write("> Data 0 end;\n")
output_file.write("> Data 1 end;\n")
output_file.write("> Data 1 end;\n")
elif hex=="4":
output_file.write("> Data 0 end;\n") #print hex 4 in binary
output_file.write("> Data 1 end;\n")
output_file.write("> Data 0 end;\n")
output_file.write("> Data 0 end;\n")
elif hex=="5":
output_file.write("> Data 0 end;\n")
output_file.write("> Data 1 end;\n")
output_file.write("> Data 0 end;\n")
output_file.write("> Data 1 end;\n")
elif hex=="6":
output_file.write("> Data 0 end;\n")
output_file.write("> Data 1 end;\n")
output_file.write("> Data 1 end;\n")
output_file.write("> Data 0 end;\n")
else:
c="invalid"
你爲什麼停六? –