简体   繁体   English

Python 3:以编程方式将十六进制数列表转换为二进制数

[英]Python 3: Programmatically converting list of hex numbers to binary

I have a list of hex numbers as strings and I want to convert all of them to binary. 我有一个十六进制数列表作为字符串 ,我想将它们全部转换为二进制。 I'm using Python 3. What's a good way to do this? 我正在使用Python 3.有什么好办法呢?

EDIT: The numbers are in the format '0x23051fb2' , ie they start with an 0x . 编辑:数字格式为'0x23051fb2' ,即它们以0x

hexlist = ['0x23051fb2', '0xAB', '0xAC']
binlist = [bin(int(x, 16)) for x in hexlist]

Now if we do print(binlist) we get: 现在,如果我们print(binlist)我们得到:

['0b100011000001010001111110110010', '0b10101011', '0b10101100']
lst = ['0x1A', '0xFF', 'FF']
print map(lambda x: bin(int(x, 16)), lst)

Output: 输出:

['0b11010', '0b11111111', '0b11111111']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM