Re: [exim] Base62 to Decimal

Página Principal
Apagar esta mensagem
Responder a esta mensagem
Autor: Phil Pennock
Data:  
Para: exim-users
CC: Grant Peel
Assunto: Re: [exim] Base62 to Decimal
On 2011-12-31 at 06:28 -0500, Phil Pennock wrote:
> I'm assuming you're after decoding the time from the first part of the
> message id, per "3.4 Message identification", and are not concerned
> about portability to MacOS?


If you do care about MacOS, then this, but I suggest coming up with a
better name for the function and constants.

----------------------------8< cut here >8------------------------------
#!/usr/bin/perl
use warnings;
use strict;

use constant BASE62DICT => join('', 0..9, 'A'..'Z', 'a'..'z');
# The approach of redefining "62" is what Exim uses. :)
use constant BASE62SIZE => $^O eq "darwin" ? 36 : 62;

sub base62_to_num {
    my @input = split(//, $_[0]);
    my $result = 0;
    foreach my $b62digit (@input) {
        my $offset = index BASE62DICT, $b62digit;
        if ($offset == -1 or $offset >= BASE62SIZE) {
            die "exception/base62_to_num: invalid base62 digit: $b62digit";
        }
        $result *= BASE62SIZE;
        $result += $offset;
    }
    return $result;
}


foreach my $arg (@ARGV) {
    if ($arg =~ /-/) {
        print "$arg :\n";
        foreach my $x (split(/-/, $arg)) {
            print "\t$x\t", base62_to_num($x), "\n";
        }
    } else {
        print $arg, "\t", base62_to_num($arg), "\n";
    }
}
----------------------------8< cut here >8------------------------------


--
https://twitter.com/syscomet