Bash base 62 to decimal conversion

I would like to reverse the operation performed by the following bash command:

$ echo $((62#a39qrT))
9207903953

i.e. convert decimal to base 62, keeping bash standard of {0..9},{a..z},{A..Z}.

I know I can do this by using bc, but I will have to manually convert each character then. For example, I do this currently:

BASE62=($(echo {0..9} {a..z} {A..Z}))
for i in $(echo "obase=62; 9207903953" | bc)
do
    echo -n ${BASE62[$i]} #Doesn't work if bc's output contains leading zeroes
done

There must be a way to do this in a less 'hackier' way. Do you know of a way to do this more efficiently?

EDIT: changed bc input

Answers


I do really appreciate the solution you came up with, and I guess there's no way around it straight with bash. Here's the little point you've missed:

BASE62=($(echo {0..9} {a..z} {A..Z}))
for i in $(bc <<< "obase=62; 9207903953"); do
    echo -n ${BASE62[$(( 10#$i ))]}
done && echo

Output:

a39qrT

Need Your Help

android: am pm format of alarm

android

I am using the alarmmanger class set method for setting the alarm. i

yaml or annotation mapping in doctrine 2

symfony2 doctrine2

Which one to choose yaml or annotation mapping in doctrine 2? I decided to go with doctrine 2 as orm in symfony framework. Using annotation based mapping for entity, I would have to write lot of co...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.