import binascii# 打开二进制文件以只读二进制模式
with open('EXCEL/文件.dat', 'rb') as file:binary_data = file.read()print(binary_data)# 将二进制数据转换为十六进制字符串
hex_data = binascii.hexlify(binary_data).decode('utf-8')
# binary_data 现在包含了文件的二进制内容
print(hex_data)
结果:
如果想转成数组形式:
import binasciiwith open('EXCEL/载荷管理单元主控boot重构文件.dat', 'rb') as file:binary_data = file.read()hex_data = binascii.hexlify(binary_data).decode('utf-8')byte_list = [hex_data[i:i + 2] for i in range(0, len(hex_data), 2)] # 转成字节数组
print(byte_list)
结果: