```
# Imports #
from __future__ import print_function
# Main #
def main():
# Variables - Local
# Main Code
print(f_NumBaseConvert(257938572394,10,62)) ## 4XYBxik
#-----------Basement------------
# Functions #
# Normal Functions
def f_NumBaseConvert(v_Num, v_BaseFrom, v_BaseTo):
"""&&Syntax: INT or STR(for non base-10); Returns new num. as STR;
Desc.: Converts a INT or STR "number" between two bases, up to base-62, of arbitrary digits.
Ref.: https://code.activestate.com/recipes/111286/
Test: print(f_NumBaseConvert(257938572394,10,62)) ## 4XYBxik"""
# Set Digit Palette
BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
v_DigitPaletteFrom = BASE62[0:v_BaseFrom] ## Init the digit palette for the from num.
v_DigitPaletteTo = BASE62[0:v_BaseTo] ## Init the digit palette for the to num.
# Convert a Negative to a Positive
v_Negative = 0 ## Init to 0 v_Negative memo
if str(v_Num)[0]=='-': ## If has a neg. sign in front.
v_Num = str(v_Num)[1:] ## Use rest of v_Num after neg. sign.
v_Negative = 1 ## Activate v_Negative memo
# Create a Working INT From the Number
v_WorkingINT = long(0) ## Init v_WorkingINT as long INT
for digit in str(v_Num):
v_WorkingINT = v_WorkingINT * len(v_DigitPaletteFrom) + v_DigitPaletteFrom.index(digit) ## Uses to increment place values as builds v_WorkingINT
# Create New Num in Desired Base
v_ResultNum =""
while v_WorkingINT > 0:
digit = v_WorkingINT % len(v_DigitPaletteTo) ## Remainder of is digit pointer
v_ResultNum = v_DigitPaletteTo[digit] + v_ResultNum ## Conc. the new digit to the result
v_WorkingINT /= len(v_DigitPaletteTo) ## Assign to v_WorkingINT the qoutient of div with len of v_DigitPaltetteTo
# Replace the Neg. Sign to the Front of New Num If Needed
if v_Negative:
v_ResultNum = "-" + v_ResultNum
return v_ResultNum
# Main Loop #
if __name__ == '__main__':
main()
```

1

Contributor0

Replies15

Views
Recommended Topics