2017-08-31 49 views
0

我硬编码以下函数将十六进制解码为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" 
+0

你为什么停六? –

回答

2

可以使用bin功能和intbase说法:

def hex_to_bin(h): 
    return bin(int(h, 16))[2:] 

example = "1a" 

for binary_digit in hex_to_bin(example): 
    print(binary_digit) 

这样做的输出:

1 
1 
0 
1 
0 

注意,这将抛出一个ValueError如果你传递无效的十六进制字符串

如果你希望它被填充到最近的nibble,你可以这样做:

def hex_to_bin(h): 
    return "{:0{}b}".format(int(h, 16), len(h) * 4) 

这将有输出:

0 
0 
0 
1 
1 
0 
1 
0 

作为证明,无论在任意长度,这些工作十六进制字符串,不只是单个数字。

这两种方法都是先将十六进制字符串解析为一个整数,然后使用int函数,然后将该整数格式化为二进制。第二个使用Python的format迷你语言,指定格式(:)应该是二进制(b),用零填充(0),它应该是四字符串长度的四倍({} - >len(h) * 4) 。 {}大括号用于表示给出的参数format。第一个使用bin函数,这是不言自明的,但它必须做[2:],因为bin函数将0b添加到生成的二进制文件的开头。 2:切片。

它应该很容易重新实现到您的原始代码。要做到这一点,你会做这样的事情:

for digit in hex_to_bin(hex_s): 
    output_file.write("> Data {} end;\n".format(digit)) 

请注意,我已经改名为您的变量hex。我建议你也这样做,因为hex是内建的Python函数(如果你打算使用十六进制,这可能会让你感到困惑)。

0

您可以使用int(..,16)将十六进制字符串解码为整数。此外,您可以例如使用一个for环获得四个不同的位,如:

def print_hex_to_atp(hex,output_file): 
    try: 
     data = int(hex,16) 
     if data < 16: 
      for i in range(3,-1,-1): 
       output_file.write("> Data {} end;\n".format((data>>i) & 1)) 
     else: 
      # do something in case the value is greater than 15 
      pass 
    except ValueError: 
     # do something in case hex is not a valid hexadecimal string 
     pass 
0
hex="4" 
base_10 = int(hex,16) 
bin = "{0:04b}".format(base_10) 
print("0x{0:02x} -> 0b{0:04b}".format(base_10)) 
for bit in bin: 
    print("> data\t{0} end".format(bit)) 
+0

https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers – Yunnosch

+1

如果使用格式说明符“:#03x”,则不需要手动添加“0x”。 –