# python十六进制转为二进制_Python从二进制字符串转换为十六进制

Python从二进制字符串转换为十六进制

12个解决方案

hex(number) -> string

Return the hexadecimal representation of an integer or long指定为2，然后hex：

>>> int('010110', 2)

22

>>> hex(int('010110', 2))

'0x16'

>>>

>>> hex(int('0000010010001101', 2))

'0x48d'

hex(number) -> string

Return the hexadecimal representation of an integer or long的文档：

hex(number) -> string

Return the hexadecimal representation of an integer or long

hex(number) -> string

Return the hexadecimal representation of an integer or long的文档：

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))

import binascii

binFile = open('somebinaryfile.exe','rb')

print binascii.hexlify(binaryData)

"{0:0>4X}".format(int("0000010010001101", 2))

'{:0{width}x}'.format(int(temp,2)), width=4)

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'

dec_str = format(int('0000010010001101', 2),'x')

dec_str.rjust(4,'0')

format(int(bits, 2), '0' + str(len(bits) / 4) + 'x')

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

b = '0000 0100 1000 1101'

h = ''.join(hex(int(a, 2))[2:] for a in b.split())

>>> 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.")

if x is 2:

binary = input("Enter number in Binary Format: ");

decimal = int(binary, 2);

print(binary,"in Decimal =",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 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));

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

02-14

03-01 538
05-30 5862
12-04 5655
08-04 4087
04-01 4459