Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

convert numbers to a different (arbitrary) base

by hostyle (Scribe)
on Oct 21, 2004 at 09:13 UTC ( [id://401098]=perlquestion: print w/replies, xml ) Need Help??

hostyle has asked for the wisdom of the Perl Monks concerning the following question:

I wanted a way to convert numbers into a different base (if thats the correct term - I'm no mathematician), where that base is determined by an array. The following works, but I'd like to know if it can be made shorter. It seems rather long for something so simple. Anyone feel like having a go?

I'll admit that this exercise is really nothing more than a silly vanity project for my website, to give me URLs somewhat similar to tinyurl.com, where numeric ids in a database will be converted to and from alphanumeric strings. Eg. http://mysite/?123 becomes http://mysite/?1Z or more interestingly http://mysite/?1000000 becomes http://mysite/?4c92

All efforts appreciated.
#!/usr/bin/perl -w use strict; my @alphanum = (0 .. 9, "a" .. "z", "A" .. "Z"); my $divisor = scalar @alphanum; #my $enc = &enc(1_000); #my $dec = &dec($enc); #print "$enc $dec\n"; for (my $x=0; $x<10_000; $x++) { print "$x -> " . &enc($x) . "\n"; } exit 0; sub dec { my $num = shift; # strip leading 0's $num =~ s/$0+//g; my ($y, $result) = (0, 0); foreach (split(//, reverse($num))) { my $found = 0; foreach my $item (@alphanum) { if ($item eq $_) { last; } $found++; } my $temp = $found * ($divisor ** $y); $result += $temp; $y++; } return $result; } sub reduce { return (int($_[0] / $divisor), $_[0] % $divisor); } sub enc { my $num = shift; my $end = ""; my ($a, $b) = reduce($num); $end = $alphanum[$b] . $end; until ($a < $divisor) { ($a, $b) = reduce($a); $end = $alphanum[$b] . $end; } $end = $alphanum[$a] . $end unless $a == 0; return $end; }

Replies are listed 'Best First'.
Re: convert numbers to a different (arbitrary) base (use Math::BaseCalc)
by grinder (Bishop) on Oct 21, 2004 at 10:00 UTC

    There's always a module do this for you...

    #! /usr/bin/perl -w use strict; use Math::BaseCalc; my $b36 = Math::BaseCalc->new( digits => [0..9, 'a' .. 'z'] ); my $b64 = Math::BaseCalc->new( digits => [0..9, 'a' .. 'z', 'A' .. 'Z' +, '-', '_' ] ); my $num; for $num( @ARGV ) { print "$num\n"; print "\t in base 36 = ", $b36->to_base($num), "\n"; print "\t in base 64 = ", $b64->to_base($num), "\n\n"; }

    The above code produces the following results:

    % ./b36 1 10 1000 1000000 100000000 1000000000 1 in base 36 = 1 in base 64 = 1 10 in base 36 = a in base 64 = a 1000 in base 36 = rs in base 64 = fE 1000000 in base 36 = lfls in base 64 = 3Q90 100000000 in base 36 = 1njchs in base 64 = 5Zu40 1000000000 in base 36 = gjdgxs in base 64 = XCIE0

    Which means that somewhere before 100 000 000 you can shave off a whole character!


    long silly update

    Over the lunch hour I pondered about the sh?aving of one character in the URLs. For certain values, the base-64 representation will catch up to the base-36 representatio. For instance, for n=1 679 616, the base-36 repesentation is 10 000 (5 characters long) and the base-64 representation is only 4 characters long (6q40).

    But bump the counter along for a while until we get to n=16 777 216, and the base-64 representation is the same length as the base-36 representation (10000 and 9xlds respectively).

    So I started wondering what value of n will have a representation in base-36 that is evermore longer than the representation in base-64?

    Just before heading out to lunch, I whipped up the following code and let it run:

    my @prev = (0,0,0); my @n = (0,0,0); my $num = 0; { $n[0] = ++$num; $n[1] = $b36->to_base($num); $n[2] = $b64->to_base($num); if( grep { $prev[$_] < length($n[$_]) } 0..2 ) { print join( ' ', @n ), "\n"; } $prev[$_] = length($n[$_]) for 0..2; redo; }

    This is an absolute prolifligate waste of cycles to search for the results. After an hour and a half it had gotten as far as a dozen or so lines of output:

    1 1 1 10 a a 36 10 A 64 1s 10 100 2s 1A 1000 rs fE 1296 100 kg 4096 35s 100 10000 7ps 2sg 46656 1000 bp0 100000 255s oqw 262144 5m9s 1000 1000000 lfls 3Q90 1679616 10000 6q40 10000000 5yc1s C9q0 16777216 9zlds 10000 60466176 100000 3CGg0 100000000 1njchs 5Zu40

    Clearly, something better than brute force was required. So then I figured a much smarter way of going about this would be to calculate the powers of 10, 36 and 64 and print them out. I then bump the power of the smallest result and start again:

    #! /usr/local/bin/perl -w use strict; use Math::BaseCalc; use List::Util qw/min/; my @base = (10, 36, 64); my @cvt = ( Math::BaseCalc->new( digits => [0..9] ), Math::BaseCalc->new( digits => [0..9, 'a' .. 'z'] ), Math::BaseCalc->new( digits => [0..9, 'a' .. 'z', 'A' .. 'Z', '-', + '_' ] ), ); my @power = (0, 0, 0); my @result = (0, 0, 0); { my $min = min( @result ); print join( ' ', map { $cvt[$_]->to_base($min) . '(' . length($cvt[$_]->to_base($min)) . ')' } 0..2 ), "\n"; for( my $j = 0; $j < @power; ++$j ) { if( $result[$j] == $min ) { $power[$j]++; $result[$j] = $base[$j] ** $power[$j]; } } redo; }

    This quickly runs off the end of 64-bit integer support on my machine but nevertheless calculates (I added the length of the representation in parentheses because it was easier on the eyeballs) the following results:

    0(1) 0(1) 0(1) 10(2) a(1) a(1) 36(2) 10(2) A(1) 64(2) 1s(2) 10(2) 100(3) 2s(2) 1A(2) 1000(4) rs(2) fE(2) 1296(4) 100(3) kg(2) 4096(4) 35s(3) 100(3) 10000(5) 7ps(3) 2sg(3) 46656(5) 1000(4) bp0(3) 100000(6) 255s(4) oqw(3) 262144(6) 5m9s(4) 1000(4) 1000000(7) lfls(4) 3Q90(4) 1679616(7) 10000(5) 6q40(4) 10000000(8) 5yc1s(5) C9q0(4) 16777216(8) 9zlds(5) 10000(5) 60466176(8) 100000(6) 3CGg0(5) 100000000(9) 1njchs(6) 5Zu40(5) 1000000000(10) gjdgxs(6) XCIE0(5) 1073741824(10) hra0hs(6) 100000(6) 2176782336(10) 1000000(7) 21LN00(6) 10000000000(11) 4ldqpds(7) 9k2-g0(6) 68719476736(11) vkhsvls(7) 1000000(7) 78364164096(11) 10000000(8) 18-TA00(7) 100000000000(12) 19xtf1ts(8) 1t8tKw0(7) 1000000000000(13) cre66i9s(8) ezkFh00(7) 2821109907456(13) 100000000(9) F3ngg00(7) 4398046511104(13) 1k4fnc6ps(9) 10000000(8) 10000000000000(14) 3jlxpt2ps(9) 2hxesG00(8) 100000000000000(15) zg3d62r5s(9) mLcguA00(8) 101559956668416(15) 1000000000(10) n5V59000(8) 281474976710656(15) 2rrvthnxts(10) 100000000(9) 1000000000000000(16) 9ugxnorjls(10) 3znWANE00(9) 3656158440062976(16) 10000000000(11) c_k6V4000(9) 10000000000000000(17) 2qgpckvng1s(11) zxL9LMg00(9)

    All of which lets us deduce the most important fact: if you have more than 78 364 164 096 URLs to represent, use a base-64 representation will give you a consistent 1-character gain in your resulting URLs. The importance of this finding should not be underestimated.

    - another intruder with the mooring of the heat of the Perl

      Perfect :)
Re: convert numbers to a different (arbitrary) base
by tilly (Archbishop) on Oct 21, 2004 at 16:00 UTC
    Doing this problem in pure Perl for small numbers is easy. Doing it in pure Perl for large numbers is hard because of round-off errors. In another language it could be easy - for instance Ruby by default uses infinite-length integers.

    So one time to take my mind off of being sick I wrote a module that makes it doable, Math::FlexibleMath::Fleximal. With that you can write the following:

    use Math::Fleximal; my $number = '123456789012345678901234567890'; my $str = dec2alpha($number); print "$str\n"; print alpha2dec($str), "\n"; # Turns a number from base 10 to base 62 sub dec2alpha { Math::Fleximal->new( shift, [0..9] )->change_flex( [0..9, 'a'..'z', 'A'..'Z'] )->to_str(); } # Turns a number from base 62 to base 10 sub alpha2dec { Math::Fleximal->new( shift, [0..9, 'a'..'z', 'A'..'Z'] )->change_flex( [0..9] )->to_str(); } __DATA__ Prints: 2AyLS9BKAMjjsWHR0 123456789012345678901234567890
    Note that if you are using this to create strings to send to other people, you should consider removing various characters from the alphabet. Like the vowels, numbers that can be mistaken for being vowels (eg 3), and letters that can be mistaken for each other (eg 1 and l).

    UPDATE: Corrected typo in module name. (Thanks tye.)

Re: convert numbers to a different (arbitrary) base
by ikegami (Patriarch) on Oct 21, 2004 at 15:35 UTC

    Your example uses base 62. Base 64 more efficient (because it's a power of 2), well tested, produces slightly smaller results.

    use MIME::Base64; $num = 123; $encoded = encode_base64(pack("V", $num), ''); $encoded =~ s/==$//; print($encoded, $/); # ewAAAA (always 6 chars) $encoded .= '=='; $num = unpack("V", decode_base64($encoded)); print($num, $/); # 123

    (If you only have int16s, lowercase 'v' (in both places) will result in 3 encoded bytes.)

    or even

    use MIME::Base64; $num = 123; $encoded = encode_base64(pack("V", $num), ''); $encoded =~ s/A+==$//; print($encoded, $/); # ew $encoded = substr($encoded.'AAAAAA', 0, 6) . '=='; $num = unpack("V", decode_base64($encoded)); print($num, $/); # 123
Re: convert numbers to a different (arbitrary) base
by Anonymous Monk on Oct 21, 2004 at 09:45 UTC
    #!/usr/bin/perl use strict; use warnings; use Inline 'BC'; # Little languages are cool. sub base { my ($number, $base) = @_; my $r = mybase ($number, $base); # bc returns groups of digits separated by spaces # if the base is larger than 16. # Mangle the return value if the base is at most 36, # but leave it like it is if the base is above 36. if ($base > 16 && $base <= 36) { $r =~ s/([1-9][0-9])/chr(ord('A')+$1-11)/ge; $r =~ s/[0\s]+//g; } $r } use Inline 'BC'; print base(1000000,36), "\n"; __DATA__ __BC__ define mybase(a, b){ obase=b return(a) } KEKR

      I can't get that to work. Any pointers?

      skynet [~/bin] 2:07pm>perl alphabc.pl Error. You have specified 'BC' as an Inline programming language. I currently only know about the following languages: C, Foo, foo If you have installed a support module for this language, try deleting + the config file from the following Inline DIRECTORY, and run again: /home/hostyle/bin/_Inline at alphabc.pl line 0 INIT failed--call queue aborted. One or more DATA sections were not processed by Inline. skynet [~/bin] 2:08pm>bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. quit skynet [~/bin] 2:09pm>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://401098]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found