#测试数据
'''
00E802370EF85C806701
460038550033678
0057029F64E45C806701
460036000249260
'''
#0x3FF mask
H3fMask = 0b1111111111
#0x3FFF mask
H3ffMask = 0b11111111111111
#0xFFFFFF mask
H6fMask = 0b111111111111111111111111
minTab = ['1','2','3','4','5','6','7','8','9','0']
def ImsiHundredsDigit(x):
return int(((x/100)%10))
def ImsiTensDigit(x):
return int(((x/10)%10))
def ImsiOnesDigit(x):
return int(((x/1)%10))
class RuimImsiType:
imsiClass = 0
imsiS2 = [0,0]
imsiS1 = [0,0,0]
imsiL1L2 = 0
imsiAddrNum = 0
imsiMcc = [0,0]
imsi = ''
tmpdata = ''
#把字符串装换成十六进制,并且输出到dataUim
def InDataDecode(dataUim,data):
tmp = 0
#get X16 data
for i in range(0,len(data),2):
tmpstr = str(data[i])+str(data[i+1])
tmp = int(tmpstr,16)
#print(tmp)
if i == 0:
dataUim.imsiClass = tmp
elif i == 2:
dataUim.imsiS2[0] = tmp
elif i == 4:
dataUim.imsiS2[1] = tmp
elif i == 6:
dataUim.imsiS1[0] = tmp
elif i == 8:
dataUim.imsiS1[1] = tmp
elif i == 10:
dataUim.imsiS1[2] = tmp
elif i == 12:
dataUim.imsiL1L2 = tmp
elif i == 14:
dataUim.imsiAddrNum = tmp
elif i == 16:
dataUim.imsiMcc[0] = tmp
elif i == 18:
dataUim.imsiMcc[1] = tmp
else:
print("error InDataDecode"+str(i))
#print('tmp= ' + str(tmp))
dataUim.tmpdata = dataUim.tmpdata + str(tmp) + ' '
def DecodeMin1(dataUim):
min1 = (dataUim.imsiS1[2] << 16 )|
(dataUim.imsiS1[1] << 8 ) |
dataUim.imsiS1[0]
min_str = min1 & H6fMask
temp = (min_str >> 14) & H3fMask
tempstr = minTab[ImsiHundredsDigit(temp)]
dataUim.imsi =dataUim.imsi + tempstr
tempstr = minTab[ImsiTensDigit(temp)]
dataUim.imsi = dataUim.imsi + tempstr
tempstr = minTab[ImsiOnesDigit(temp)]
dataUim.imsi = dataUim.imsi + tempstr
temp = ((min_str & H3ffMask) >> 10) & 0b1111
#data
if temp ==10:
dataUim.imsi = dataUim.imsi + '0'
else:
dataUim.imsi = dataUim.imsi + str(temp)
temp = min_str & H3fMask
tempstr = minTab[ImsiHundredsDigit(temp)]
dataUim.imsi =dataUim.imsi + tempstr
tempstr = minTab[ImsiTensDigit(temp)]
dataUim.imsi = dataUim.imsi + tempstr
tempstr = minTab[ImsiOnesDigit(temp)]
dataUim.imsi = dataUim.imsi + tempstr
def DataCheck():
if len(data)!= 20:
print('data error , len error')
sys.exit()
if __name__ == '__main__':
#data = "0057029F64E45C806701"
#data = "00E802370EF85C806701"
DataCheck()
dataUim = RuimImsiType
InDataDecode(dataUim,data)
DecodeMcc(dataUim)
Decode1112(dataUim)
DecodeMin2(dataUim)
DecodeMin1(dataUim)
print('input data: '+ str(data))
# print(dataUim.tmpdata)
print('imsi : ' + str(dataUim.imsi))
higMask = 0b11110000
lowMask = 0b00001111
def higer_bcd(x):
return (x & higMask) >> 4
def lower_bcd(x):
return (x & lowMask)
if __name__ == '__main__':
data = "0057029F64E45C806701"
data2 = [73,6,17,96,70,99,34,8]
odd_digits = data2[0] & 0b1000
print('odd_digits = ' + str(odd_digits))
output=[]
for i in range(0,len(data2)):
if i == 0:
output.append(higer_bcd(data2[i]))
elif i == len(data2)-1:
output.append(lower_bcd(data2[i]))
if odd_digits!=0:
output.append(higer_bcd(data2[i]))
else:
output.append(lower_bcd(data2[i]))
output.append(higer_bcd(data2[i]))
print(str(output))