#coding:utf-8 __author__ = 'Mochazz' encode = '差分曼彻斯特编码' dic = { '0':'0000', '1':'0001', '2':'0010', '3':'0011', '4':'0100', '5':'0101', '6':'0110', '7':'0111', '8':'1000', '9':'1001', 'A':'1010', 'B':'1011', 'C':'1100', 'D':'1101', 'E':'1110', 'F':'1111', } string1 = '3EAAAAA56A69AA55A95995A569AA95565556';//题目 # string1 = '3EAAAAA56A69AA556A965A5999596AA95656'//所求 list1 = [] for i in string1: list1.append(dic[i]) list1 = ('').join(list1) print('%s每位数字转成二进制:'%string1) print(list1) l = [] flag = list1[:2] if flag == '00' or flag == '11': l.append('0') else: l.append('1') i = 2 while i < len(list1): if list1[i-2:i] == list1[i:i+2]: l.append('0') else: l.append('1') i += 2 l = ('').join(l) print('转换结果:') print(l) dic2 = {value:key for key,value in dic.items()} i = 0 flag = [] while i < len(l): flag.append(dic2[l[i:i+4]]) i += 4 flag = (''.join(flag)) print('turn to hex') print('0x'+flag) print('The ID is:') print('0x'+flag[6:14])