Clock generator 555: use a period almost the same as clock_gen.
[trinary.git] / tools / base_converter.py
bloba521b214ee81bce1ca36bf6bc32f9197a8f5bddd
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
50 if base_to < 0 and base_to*-1%2 != 1:
51 print "base_to is even: negative bases must be odd integers"
52 raise SystemExit
53 elif base_to < 0:
54 magnitude_t = (base_to*-1 - 1)/2
55 else:
56 magnitude_t = base_to
58 sum = 0 # base 10 equivalent summation
59 neg = 1 # used when 'i' is encountered, it negates the previous digit
60 sign = 1 # sign of summation
61 count = 1 # amount to multiply next digit by
62 prev = 1 # the value of the next digit
63 cur = 0 # current digit
65 if magnitude_f >= MIN_VAL:
66 max_val = magnitude_f - MIN_VAL
68 for i in range(len(value) - 1, -1, -1):
70 if abs(base_frm) == 3:
71 # Base 3 conversion
72 if value[i] in Trits.trit_integer:
73 sum = sum + Trits.trit_integer[value[i]]*count
74 count = count*abs(base_frm)
75 else:
76 print "%s invalid input", value[i]
77 raise SystemExit
79 elif value[i].isdigit():
80 # 0 <-> 9
81 cur = int(value[i])
83 if cur > magnitude_f:
84 print "%s: invalid input", value[i]
85 raise SystemExit
86 if i != len(value) -1:
87 sum = sum + prev*neg*count
89 # reset variables to appropiate values
90 neg = 1
91 count = count*abs(base_frm)
93 prev = cur
95 elif value[i] == '-' and i == len(value) - 1:
96 # negate the whole number
97 sign = -1
98 elif value[i] == 'i':
99 # negate prev number
100 neg = -1
101 elif magnitude_f >= MIN_VAL and value[i].isalpha():
102 # 10 <-> magnitude_f
103 cur = ord(value[i].upper()) - LOW_BOUND
105 if cur > magnitude_f:
106 print "%s: invalid input", value[i]
107 raise SystemExit
109 if i != len(value) -1:
110 sum = sum + prev*neg*count
112 # reset variables to appropriate values
113 neg = 1
114 count = count*abs(base_frm)
116 prev = cur
118 else:
119 print "%s: invalid input", value[i]
120 raise SystemExit
122 # sum up remaining digit
123 sum = sum + prev*neg*count
124 sum = sign*sum
126 # return base 10 if desired base is balanced
127 if base_frm < 0:
128 return "" + sum
130 # compute unbalanced conversion
131 result = ""
132 quotient = sum
133 remainder = 0
135 while quotient != 0:
136 remainder = quotient%magnitude_t
137 quotient = quotient/magnitude_t
138 result = str(remainder) + result
140 return result
142 ''' NOTES
143 useful things to know for implementation
144 char to int: ord('a') = 97 ASCII
145 int to char: chr(97) ASCII
146 char to int: int('4')
147 a = "hi6"
148 a[0].isalpha() => true
149 a[2].isdigit() => true
150 a[0].upper() => "H"
151 a[2].upper() => "1"
153 s.split(".") => returns list of strings broken up by "."
154 "1010.3930" => ["1010", "3930"]
156 s = ".".join(lis_digits) => combine elements in list by "."
159 if __name__ == "__main__":
160 print int_cnvrt("10i", -3, 10)