Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Fast - Compact That String

by Eliya (Vicar)
on Feb 09, 2012 at 22:12 UTC ( [id://952861]=note: print w/replies, xml ) Need Help??


in reply to Fast - Compact That String

my @ch = (' ', '0'..'9', 'A'..'Z'); my $base = @ch; my $num = 2_565_726_408; my $s = ''; while ($num > 0) { $s = $ch[$num % $base] . $s; $num = int($num / $base); } print $s; # ZZZZZZ

(for small numbers you could space-pad the string on the left, if you like)

And the reciprocal procedure (string to number) could be

my @val; $val[ ord($ch[$_]) ] = $_ for 0..$#ch; my $s = ... my $num = 0; my $m = 1; for (reverse split //, $s) { $num += $m * $val[ ord($_) ]; $m *= $base; } print $num;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-18 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found