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

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

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

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

sylvain asked 2020-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 2020-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 2020-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 2020-02-04T08:13:11Z

6 votes

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

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

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

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

3 votes

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

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

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

Sam Palmer answered 2020-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 2020-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 2020-02-04T08:15:02Z

0 votes

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

Dmitry Sobolev answered 2020-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 2020-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 2020-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 2020-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 2020-02-04T08:16:43Z

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

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值