-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathconvertWeight.py
53 lines (45 loc) · 1.26 KB
/
convertWeight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
''' mbinary
#########################################################################
# File : num_weight.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-05-19 21:36
# Description:
#########################################################################
'''
def covert(s, basefrom=10, baseto=2):
return d2n(n2d(s, basefrom), baseto)
def n2d(s, base=16):
''' num of base_n(n<36) to decimal'''
dic = {chr(i+ord('0')): i for i in range(10)}
s = s.upper()
if base > 10:
dic.update({chr(i+ord('A')): i+10 for i in range(26)})
# if base in [16,8,2] :
# p=max(map(s.find,'OBX'))
# s=s[p+1:] #remove prefix of hex or bin or oct
rst = 0
for i in s:
rst = dic[i]+rst*base
return rst
def d2n(n, base=16):
''' num of base_n(n<36) to decimal'''
dic = {i: chr(i+ord('0')) for i in range(10)}
if base > 10:
dic.update({i+10: chr(i+ord('A')) for i in range(26)})
rst = []
while n != 0:
i = int(n/base)
rst.append(dic[n-i*base])
n = i
return ''.join(rst[::-1])
'''
>>> n2d(str(d2n(4001)))
4001
>>> d2n(n2d(str(4001)),2)
'100000000000001'
>>> covert('4001',16,2)
'100000000000001'
'''