Python|将十六进制转换为二进制的方法
将十六进制转换为二进制是一个非常常见的编程问题。在本文中,我们将看到一些解决上述问题的方法。
方法 #1:使用 bin 和 zfill
# Python code to demonstrate
# conversion of a hex string
# to the binary string
# Initialising hex string
ini_string = "1a"
scale = 16
# Printing initial string
print ("Initial string", ini_string)
# Code to convert hex to binary
res = bin(int(ini_string, scale)).zfill(8)
# Print the resultant string
print ("Resultant string", str(res))
输出:
Initial string 1a
Resultant string 00b11010
方法#2:使用朴素方法
# Python code to demonstrate
# conversion of hex string
# to binary string
import math
# Initialising hex string
ini_string = "1a"
# Printing initial string
print ("Initial string", ini_string)
# Code to convert hex to binary
n = int(ini_string, 16)
bStr = ''
while n > 0:
bStr = str(n % 2) + bStr
n = n >> 1
res = bStr
# Print the resultant string
print ("Resultant string", str(res))
输出:
Initial string 1a
Resultant string 11010
方法 #3:使用 .format
# Python code to demonstrate
# conversion of hex string
# to binary string
import math
# Initialising hex string
ini_string = "1a"
# Printing initial string
print ("Initial string", ini_string)
# Code to convert hex to binary
res = "{0:08b}".format(int(ini_string, 16))
# Print the resultant string
print ("Resultant string", str(res))
输出:
Initial string 1a
Resultant string 00011010