一个只有0和1的数组转16进制字符串
时间: 2023-11-29 10:53:59 浏览: 46
C# 16进制与字符串、字节数组之间的转换
以下是Python代码实现:
```python
def binary_to_hex(binary):
# 检查二进制数组长度是否为4的倍数
if len(binary) % 4 != 0:
binary = [0] * (4 - (len(binary) % 4)) + binary
# 将二进制数组转换为16进制字符串
hex_str = ''
for i in range(0, len(binary), 4):
hex_digit = 8 * binary[i] + 4 * binary[i+1] + 2 * binary[i+2] + binary[i+3]
hex_str += hex(hex_digit)[2:]
return hex_str.upper()
```
示例:
```python
>>> binary = [1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1]
>>> binary_to_hex(binary)
'A58C1'
```
阅读全文