?
Solved

Convert base 10 number to Base 62 within a shell script.

Posted on 2005-03-26
12
Medium Priority
?
2,287 Views
Last Modified: 2013-12-26
Convert base 10 number to Base 62 within a shell script.

VAR_1="2938567234976527896527965"
(your input suggestion)
echo $VAR_2
(some alphanumeric string)

I have looked high and low for base 62 convertors as at least examples.
The numbers to be converted within a bash script (I'll add it as a function i think) are really really big/long. I want these numbers converted to base 62 (A-Z,a-z,0-9)

NOTE: some of these number start with a "0" ie: 0387465278652786521 This  can be ignored, just wanted to forewarn ya, in case it effects the smoothness I'd like to see ;)

further stuff ...  this is the function that makes the number in the first place.

random_string()
{
RAND_1=`cat /dev/random | od | head -n 1`
RAND_2="$RANDOM"
RAND_3=`date +%N`

echo -n $RAND_1 > tmp.data
echo -n $RAND_2 >> tmp.data
echo -n $RAND_3 >> tmp.data
sed 's/ //g' tmp.data > tmp.data2
cat tmp.data2 > tmp.data
sort tmp.data > tmp.data2
uniq tmp.data2 > tmp.data
TMP=`cat tmp.data | sed 's/0000000//g' | sed 's/000//g'`
echo $TMP
}
                  
      

0
Comment
Question by:ALEx604
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:zdes
ID: 13638755
In pure shell, I'd split the string into chunks:

2938567234976527896527965 = 29385* 10000* 10000* 10000* 10000 + 67234* 10000* 10000* 10000 + 97652 * 10000 * 10000 +78965 * 100000 + 27965

and wrote functions that convert smaller nums into base 62, multiply by 10000 (in base 62) and add base 62 nums.

The way you wrote your digits in base 62,  A+A = A  and 9+B = BA
0
 
LVL 21

Expert Comment

by:tfewster
ID: 13640853
VAR_2=`echo "obase=62;$VAR_1"|bc`
0
 
LVL 2

Expert Comment

by:zdes
ID: 13641259
:-). I knew that there was a utility, but when I answered, for the life of me I could not remember it. Cool.
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 6

Expert Comment

by:Talmash
ID: 13642258
great !
0
 
LVL 2

Author Comment

by:ALEx604
ID: 13646930
# VAR_1="02541715501615734560505676316071404145100325623172151874"
# VAR_2=`echo "obase=62;$VAR_1"|bc`
# echo $VAR_2
04 18 33 42 52 46 15 11 48 03 04 16 20 18 31 20 61 04 13 52 28 11 1\ 4 06 49 41 52 08 25 31 60
#

Not exactly what I was looking for, although I am glad to see the output doesn't seem to be effected by the begining "0"

What I need is a way to convert it to a string containing the alphanumerics ie:
CHR="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z"

without the "\" within it.

Can the command "bc"  be front loaded with this set of characters?

0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 600 total points
ID: 13647512
Here's a perl script to do it.  Run "b62cvt 02541715501615734560505676316071404145100325623172151874" and it will output the base-62 version.

#!/bin/perl
# b62cvt: converts argument to base-62 representation
scalar(@ARGV) == 1 || die "Expected exactly one numerical argument\n";
my $cvtstr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
my $cmdstr = "echo \"obase=62;$ARGV[0]\" | bc";
$_ = `$cmdstr`;
while (s/^\s*(\d+)\s*//) {
    print substr($cvtstr, $1, 1);
}
print "\n";
0
 
LVL 2

Author Comment

by:ALEx604
ID: 13647642
Hmmm maybe something along this line... needs tweaking.

random_string()
{
RAND_1=`cat /dev/random | od | head -n 1`
RAND_2="$RANDOM"
RAND_3=`date +%N`

echo -n $RAND_1 > tmp.data
echo -n $RAND_2 >> tmp.data
echo -n $RAND_3 >> tmp.data
sed 's/ //g' tmp.data > tmp.data2
cat tmp.data2 > tmp.data
sort tmp.data > tmp.data2
uniq tmp.data2 > tmp.data
TMP=`cat tmp.data | sed 's/0000000//g' | sed 's/000//g'`
echo $TMP <
perl -e 'scalar(@ARGV) == 1 || die "Expected exactly one numerical argument\n"; my $cvtstr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; my $cmdstr = "echo \"obase=62;$ARGV[0]\" | bc"; $_ = `$cmdstr`;
while (s/^\s*(\d+)\s*//) {
    print substr($cvtstr, $1, 1);
}
print "\n";'
}
0
 
LVL 2

Author Comment

by:ALEx604
ID: 13647668
Reason for the tweak...

syntax error near unexpected token `newline'
 line ^: `echo $TMP <'
0
 
LVL 21

Accepted Solution

by:
tfewster earned 800 total points
ID: 13647673
Hmmm, I hadn't noticed the spurious "\ " that bc put in...We can get rid of it, but it would be better to find out _why_...

#!/usr/bin/ksh

# Convert number to Base62 & remove spurious "\" chars
VAR_1="02541715501615734560505676316071404145100325623172151874"
VAR_2=`echo "obase=62;$VAR_1"|bc|sed -e 's/\\\\\ //g' -e 's/\\\\//g'`
echo $VAR_2

# For testing the charmap, let's stick a "0" in there somewhere
VAR_2="04 18 33 42 52 46 15 11 48 03 04 16 20 18 31 20 61 04 13 52 28 11 14 06 49 41 52 08 25 31 60 00"
echo $VAR_2

CHR="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b
 c d e f g h i j k l m n o p q r s t u v w x y z"

# Load CHR into an array
INDEX=0
for MAP in $CHR
do
  CHRLIST[$INDEX]=$MAP
  let INDEX=INDEX+1
done

# Now print the Base62 number as characters
for DIGIT in $VAR_2
do
  printf ${CHRLIST[$DIGIT]}
#  echo $DIGIT ${CHRLIST[$DIGIT]}
done
0
 
LVL 2

Author Comment

by:ALEx604
ID: 13647682
Expected exactly one numerical argument
 

Piped the echo $TMP...
echo $TMP <
changed to
echo $TMP |
0
 
LVL 2

Author Comment

by:ALEx604
ID: 13647702
tfewster: SWEET!

0
 
LVL 21

Expert Comment

by:tfewster
ID: 13647705
If you use perl, you'll still need the "sed s/\\..." bit, as perl appears to barf on the "\" character "bc" produces
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question