Add order details for logic board order.
[trinary.git] / tools / base_converter.py
blob7e41b07c128b888b16ada912228e3728e6de37ae
1 #!env python
2 # vim: set fileencoding=utf8
3 # Created: April 29, 2008
4 # Created by: Antonio Chavez
6 # Base converter
8 import sys, os
9 import Trits
11 MIN_VAL = 10
12 LOW_BOUND = ord('A') + MIN_VAL
14 def int_cnvrt(value, base_frm, base_to):
15 ''' int_cvrt: convert the number to the left of the decimal place
16 value: string containing value to convert
17 from: what base to convert from. Can be positive and negative.
18 Negative numbers will represent balanced base. Positive
19 will represent unbalanced base.
20 to: what base to convert to.
22 Example: 3 = unbalanced base 3 (0, 1, 2)
23 -3 = balanced base 3 (-1, 0, 1)
24 Balanced bases can only be odd integers, for the obivious reason
25 that even numbers are not good candidates for balanced numbering
26 Example: -4 = (-1, 0, 1, 2) or (-2, -1, 0, 1). Either system is
27 not balanced. Bases must also have a magnitude greater than 1.
28 For balanced bases greater than 3, a negative number is represented
29 with an 'i' next to it.
30 Example: 4i21 => 4(-2)1
31 Currently conversion balanced bases is not supported. If balanced
32 base is entered, the result will be returned in base 10.
33 A negative sign before the number will negate the result.
34 '''
36 # check for magnitude greater than 1
37 if abs(base_frm) < 2 or abs(base_to) < 2:
38 print "bases must have magnitude greater than 1"
39 raise SystemExit
41 # check for a balanced negative base and derive magnitude
42 if base_frm < 0 and base_frm*-1%2 != 1:
43 print "base_from is even: negative bases must be odd integers"
44 raise SystemExit
45 elif base_frm < 0:
46 magnitude_f = (base_frm*-1 - 1)/2
47 else:
48 magnitude_f = base_frm
49 #magnitude_f = abs(base_frm)
51 if base_to < 0 and base_to*-1%2 != 1:
52 print "base_to is even: negative bases must be odd integers"
53 raise SystemExit
54 elif base_to < 0:
55 magnitude_t = (base_to*-1 - 1)/2
56 else:
57 magnitude_t = base_to
58 #magnitude_t = abs(base_to)
60 sum = 0 # base 10 equivalent summation
61 neg = 1 # used when 'i' is encountered, it negates the previous digit
62 sign = 1 # sign of summation
63 count = 1 # amount to multiply next digit by
64 prev = 1 # the value of the next digit
65 cur = 0 # current digit
67 if magnitude_f >= MIN_VAL:
68 max_val = magnitude_f - MIN_VAL
70 for i in range(len(value) - 1, -1, -1):
72 if base_frm == -3:
73 # Base 3 conversion
74 if value[i] in Trits.trit_integer:
75 sum = sum + (Trits.trit_integer[value[i]])*count
76 count = count*abs(base_frm)
77 else:
78 print "%s invalid input", value[i]
79 raise SystemExit
81 elif value[i].isdigit():
82 # 0 <-> 9
83 cur = int(value[i])
85 if cur > magnitude_f:
86 print "%s: invalid input", value[i]
87 raise SystemExit
88 if i != len(value) -1:
89 sum = sum + prev*neg*count
91 # reset variables to appropiate values
92 neg = 1
93 count = count*abs(base_frm)
95 prev = cur
97 elif value[i] == '-' and i == len(value) - 1:
98 # negate the whole number
99 sign = -1
100 elif value[i] == 'i':
101 # negate prev number
102 neg = -1
103 elif magnitude_f >= MIN_VAL and value[i].isalpha():
104 # 10 <-> magnitude_f
105 cur = ord(value[i].upper()) - LOW_BOUND
107 if cur > magnitude_f:
108 print "%s: invalid input", value[i]
109 raise SystemExit
111 if i != len(value) -1:
112 sum = sum + prev*neg*count
114 # reset variables to appropriate values
115 neg = 1
116 count = count*abs(base_frm)
118 prev = cur
120 else:
121 print "%s: invalid input", value[i]
122 raise SystemExit
124 if base_frm == -3:
125 return "" + str(sum)
127 # sum up remaining digit
128 sum = sum + prev*neg*count
129 sum = sign*sum
131 # return base 10 if desired base is balanced
132 if base_frm < 0:
133 return "" + str(sum)
135 # compute unbalanced conversion
136 result = ""
137 quotient = sum
138 remainder = 0
140 while quotient != 0:
141 remainder = quotient%magnitude_t
142 quotient = quotient/magnitude_t
143 result = str(remainder) + result
145 return result
147 ''' NOTES
148 useful things to know for implementation
149 char to int: ord('a') = 97 ASCII
150 int to char: chr(97) ASCII
151 char to int: int('4')
152 a = "hi6"
153 a[0].isalpha() => true
154 a[2].isdigit() => true
155 a[0].upper() => "H"
156 a[2].upper() => "1"
158 s.split(".") => returns list of strings broken up by "."
159 "1010.3930" => ["1010", "3930"]
161 s = ".".join(lis_digits) => combine elements in list by "."
164 if __name__ == "__main__":
165 print int_cnvrt("8", 10, 2)
167 while True:
168 print ">> ",
169 line = unicode(sys.stdin.readline(), "utf8")
170 if len(line) == 0:
171 break
172 line = line.strip()
173 print
175 val, frm, to = line.split(",")
176 frm = int(frm)
177 to = int(to)
179 try:
180 print int_cnvrt(val, frm, to)
181 except:
182 print "An error occured:"
183 traceback.print_exc()