[英]Python conversion from binary string to hexadecimal
如何在 Python 中將二進制字符串轉換為相應的十六進制值?
我有0000 0100 1000 1101
並且我想獲得048D
我正在使用 Python 2.6。
int
給定基數 2 然后hex
:
>>> int('010110', 2)
22
>>> hex(int('010110', 2))
'0x16'
>>>
>>> hex(int('0000010010001101', 2))
'0x48d'
int
的文檔:
int(x[, base]) -> integer Convert a string or number to an integer, if possible. A floating
point 參數將被截斷為零(這不包括浮點數的字符串表示!)轉換字符串時,使用可選的基數。 轉換非字符串時提供基數是錯誤的。 如果基數為零,則根據字符串內容猜測正確的基數。 如果參數超出整數范圍,則將返回一個 long 對象。
hex
文檔:
hex(number) -> string Return the hexadecimal representation of an integer or long
整數。
bstr = '0000 0100 1000 1101'.replace(' ', '')
hstr = '%0*X' % ((len(bstr) + 3) // 4, int(bstr, 2))
使用 python 的 binascii 模塊
import binascii
binFile = open('somebinaryfile.exe','rb')
binaryData = binFile.read(8)
print binascii.hexlify(binaryData)
將二進制轉換為十六進制而不忽略前導零:
您可以像這樣使用 format() 內置函數:
"{0:0>4X}".format(int("0000010010001101", 2))
不使用雜亂的串聯和填充:
'{:0{width}x}'.format(int(temp,2)), width=4)
將給出保留填充的十六進制表示
在 python3 上使用hexlify函數:
import binascii
def bin2hex(str1):
bytes_str = bytes(str1, 'utf-8')
return binascii.hexlify(bytes_str)
a="abc123"
c=bin2hex(a)
c
會給你回:
b'616263313233'
你可以得到它的字符串,如:
c.decode('utf-8')
給出:
'616263313233'
此概述可能對某人有用:python中的 bin、dec、hex 在 python 中的 bin、dec、hex 之間進行轉換。
我會做:
dec_str = format(int('0000010010001101', 2),'x')
dec_str.rjust(4,'0')
結果:'048d'
format(int(bits, 2), '0' + str(len(bits) / 4) + 'x')
假設它們按 4 分組並由空格分隔。 這將保留前導 0。
b = '0000 0100 1000 1101'
h = ''.join(hex(int(a, 2))[2:] for a in b.split())
無論出於何種原因,我對其中的一些答案有疑問,我已經為自己編寫了幾個幫助函數,所以如果您有像我一樣的問題,請嘗試一下。
def bin_string_to_bin_value(input):
highest_order = len(input) - 1
result = 0
for bit in input:
result = result + int(bit) * pow(2,highest_order)
highest_order = highest_order - 1
return bin(result)
def hex_string_to_bin_string(input):
lookup = {"0" : "0000", "1" : "0001", "2" : "0010", "3" : "0011", "4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111", "8" : "1000", "9" : "1001", "A" : "1010", "B" : "1011", "C" : "1100", "D" : "1101", "E" : "1110", "F" : "1111"}
result = ""
for byte in input:
result = result + lookup[byte]
return result
def hex_string_to_hex_value(input):
bin_string = hex_string_to_bin_string(input)
bin_value = bin_string_to_bin_value(bin_string)
return hex(int(bin_value, 2))
他們似乎工作得很好。
print hex_string_to_hex_value("FF")
print hex_string_to_hex_value("01234567")
print bin_string_to_bin_value("11010001101011")
結果:
0xff
0x1234567
0b11010001101011
要將二進制字符串轉換為十六進制字符串,我們不需要任何外部庫。 使用格式化的字符串文字(稱為 f 字符串)。 這個特性是在 python 3.6 (PEP 498) 中添加的
>>> bs = '0000010010001101'
>>> hexs = f'{int(bs, 2):X}'
>>> print(hexs)
>>> '48D'
如果要小寫的十六進制字符串,請使用小“x”,如下所示
f'{int(bs, 2):x}'
其中 f-string 中的bs
是一個變量,其中包含事先分配的二進制字符串
f-strings 丟失更有用和有效。 它們沒有得到充分利用。
>>> import string
>>> s="0000 0100 1000 1101"
>>> ''.join([ "%x"%string.atoi(bin,2) for bin in s.split() ] )
'048d'
>>>
或
>>> s="0000 0100 1000 1101"
>>> hex(string.atoi(s.replace(" ",""),2))
'0x48d'
x = int(input("press 1 for dec to oct,bin,hex \n press 2 for bin to dec,hex,oct \n press 3 for oct to bin,hex,dec \n press 4 for hex to bin,dec,oct \n"))
if x is 1:
decimal =int(input('Enter the decimal number: '))
print(bin(decimal),"in binary.")
print(oct(decimal),"in octal.")
print(hex(decimal),"in hexadecimal.")
if x is 2:
binary = input("Enter number in Binary Format: ");
decimal = int(binary, 2);
print(binary,"in Decimal =",decimal);
print(binary,"in Hexadecimal =",hex(decimal));
print(binary,"in octal =",oct(decimal));
if x is 3:
octal = input("Enter number in Octal Format: ");
decimal = int(octal, 8);
print(octal,"in Decimal =",decimal);
print(octal,"in Hexadecimal =",hex(decimal));
print(octal,"in Binary =",bin(decimal));
if x is 4:
hex = input("Enter number in hexa-decimal Format: ");
decimal = int(hex, 16);
print(hex,"in Decimal =",decimal);
print(hex,"in octal =",oct(decimal));
print(hex,"in Binary =",bin(decimal));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.