Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-03-26
12
Medium Priority
?
2,365 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

578 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