★★★ 个人博客导读首页—点击此处 ★★★
如0x1234,写入到文件应该是hex:[00 00 12 34]
import sys
import os
import struct
def tran_hex_to_binary(x2):
temp_list = []
str_x2 = str(hex(x2))[2:].rstrip()
if (len(str_x2))%2 == 1:
str_x2 = "0" + str_x2
for i in range(0, int((8 - len(str_x2))/2)):
temp_list.append("0x0")
for i in range(0, int((len(str_x2))/2 + (len(str_x2))%2 )):
temp_str = "0x" + str_x2[i*2:i*2+2]
temp_list.append(temp_str)
print(temp_list)
with open("1.txt", "wb") as fp:
for x in temp_list:
s = struct.pack('B',int(x,16))
fp.write(s)
tran_hex_to_binary(0x1234)
#tran_hex_to_binary(0x234)
快速链接:
.
个人博客笔记导读目录(全部)