2 # vim: set fileencoding=utf8
3 # Created: April 29, 2008
4 # Created by: Antonio Chavez
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.
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"
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"
46 magnitude_f
= (base_frm
*-1 - 1)/2
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"
55 magnitude_t
= (base_to
*-1 - 1)/2
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):
74 if value
[i
] in Trits
.trit_integer
:
75 sum = sum + (Trits
.trit_integer
[value
[i
]])*count
76 count
= count
*abs(base_frm
)
78 print "%s invalid input", value
[i
]
81 elif value
[i
].isdigit():
86 print "%s: invalid input", value
[i
]
88 if i
!= len(value
) -1:
89 sum = sum + prev
*neg
*count
91 # reset variables to appropiate values
93 count
= count
*abs(base_frm
)
97 elif value
[i
] == '-' and i
== len(value
) - 1:
98 # negate the whole number
100 elif value
[i
] == 'i':
103 elif magnitude_f
>= MIN_VAL
and value
[i
].isalpha():
105 cur
= ord(value
[i
].upper()) - LOW_BOUND
107 if cur
> magnitude_f
:
108 print "%s: invalid input", value
[i
]
111 if i
!= len(value
) -1:
112 sum = sum + prev
*neg
*count
114 # reset variables to appropriate values
116 count
= count
*abs(base_frm
)
121 print "%s: invalid input", value
[i
]
127 # sum up remaining digit
128 sum = sum + prev
*neg
*count
131 # return base 10 if desired base is balanced
135 # compute unbalanced conversion
141 remainder
= quotient
%magnitude_t
142 quotient
= quotient
/magnitude_t
143 result
= str(remainder
) + result
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')
153 a[0].isalpha() => true
154 a[2].isdigit() => true
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)
169 line
= unicode(sys
.stdin
.readline(), "utf8")
175 val
, frm
, to
= line
.split(",")
180 print int_cnvrt(val
, frm
, to
)
182 print "An error occured:"
183 traceback
.print_exc()