"""
16进制转10进制
"""
# str="A5 42 D2 00 4A 00 01 00 01 F4 00 32 00 32 00 50 01 0C 00 01 00 01 F4 00 32 00 32 00 50 01 01 00 01 00 01 F4 01 F4 01 F4 01 F4 01 F4 00 32 00 32 00 32 00 32 00 32 00 32 00 32 00 32 00 32 00 32 00 50 01 00 00 01 F4 00 32 00 32 50 00 00 00 D2 41 5A "
str=""
with open("bitChange",mode="r",encoding="UTF-8") as f: #从文件里读数据
str= f.read()
lst=str.split(" ")#根据空格切割
lst=lst[5:len(lst)-4]#根据自己实际情况来做,我这里是根据我这边的通讯协议格式来的
dic={}
count=0
for c in lst:
dic[count]=c
count+=1
print(dic)
print(lst)
#16进制转10进制
new_Lst=[]#得到需要的16进制
starW= int(input("请输入起始位置:"))-1
lenW=int(input("请输入长度:"))
new_Lst=lst[starW:lenW+starW]
new_Str="".join(new_Lst)
print("16进制:{}".format(" ".join(new_Lst)))
new_Str=new_Str[::-1]
resoult={}
resoultCount=0
strs=""
while resoultCount<len(new_Str)-1:
if new_Str[resoultCount]=="F":
resoult[resoultCount]=15
elif new_Str[resoultCount]=="E":
resoult[resoultCount] = 14
elif new_Str[resoultCount] == "D":
resoult[resoultCount] = 13
elif new_Str[resoultCount] == "C":
resoult[resoultCount] = 12
elif new_Str[resoultCount] == "B":
resoult[resoultCount] = 11
elif new_Str[resoultCount] == "A":
resoult[resoultCount] = 10
else:
resoult[resoultCount] = new_Str[resoultCount]
resoultCount+=1
sum=0
for k,v in resoult.items():
sum+=int(v)*16**k
print("10进制:{}".format(sum))
进制之间的转换可以访问