700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python十六进制转为二进制_Python从二进制字符串转换为十六进制

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

时间:2019-08-15 13:46:08

相关推荐

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

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

如何在Python中将二进制字符串转换为相应的十六进制值?

我有0000 0100 1000 1101,我想得到048D我正在使用Python 2.6。

sylvain asked -02-04T08:12:00Z

12个解决方案

59 votes

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

点 参数将被截断为零(不包含字符串) 表示浮点数!)转换字符串时, 采用 可选的基础。 转换基数时提供基数是错误的 非字符串。 如果基数为零,则根据 字符串内容。 如果参数超出整数范围,则a 将返回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

整数。

Eli Bendersky answered -02-04T08:12:35Z

26 votes

bstr = '0000 0100 1000 1101'.replace(' ', '')

hstr = '%0*X' % ((len(bstr) + 3) // 4, int(bstr, 2))

Ignacio Vazquez-Abrams answered -02-04T08:12:51Z

14 votes

使用python的binascii模块

import binascii

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

binaryData = binFile.read(8)

print binascii.hexlify(binaryData)

user3394040 answered -02-04T08:13:11Z

6 votes

在不忽略前导零的情况下将Binary转换为十六进制:

您可以使用如下所示的format()内置函数:

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

Gareth Williams answered -02-04T08:13:36Z

3 votes

不使用混乱的串联和填充:

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

将给出保留了填充的十六进制表示

Sam Palmer answered -02-04T08:14:00Z

1 votes

在使用hexlify函数的python3上:

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'

babis21 answered -02-04T08:14:33Z

1 votes

此概述对某人可能有用:python中的bin,dec,hex在python中的bin,dec,hex之间进行转换。

我会做:

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

dec_str.rjust(4,'0')

结果:“ 048d”

Tomas answered -02-04T08:15:02Z

0 votes

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

Dmitry Sobolev answered -02-04T08:15:18Z

0 votes

不管出于什么原因,我对其中的一些答案都有疑问,我已经为自己编写了一些辅助函数,因此,如果您遇到像我一样的问题,请尝试一下。

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

onaclov2000 answered -02-04T08:15:47Z

-1 votes

假设它们按4分组并用空格分隔。 这将保留前导0。

b = '0000 0100 1000 1101'

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

Tor Valamo answered -02-04T08:16:07Z

-3 votes

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

ghostdog74 answered -02-04T08:16:27Z

-3 votes

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

YogVj answered -02-04T08:16:43Z

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。