Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: exim: users

Base62 to Decimal

 

 

exim users RSS feed   Index | Next | Previous | View Threaded


gpeel at thenetnow

Dec 30, 2011, 8:26 AM

Post #1 of 5 (545 views)
Permalink
Base62 to Decimal

Hi all,

Happy Holidays :-)

Does anyone have a simple perl snippett that accepts base62 code (as in exim mail IDs) and converts it to decimal ?

-Grant
--
## List details at https://lists.exim.org/mailman/listinfo/exim-users
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/


exim-users at spodhuis

Dec 31, 2011, 3:28 AM

Post #2 of 5 (529 views)
Permalink
Re: Base62 to Decimal [In reply to]

On 2011-12-30 at 11:26 -0500, Grant Peel wrote:
> Does anyone have a simple perl snippett that accepts base62 code (as
> in exim mail IDs) and converts it to decimal ?

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?

The alphabet is:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

This is optimised for clarity rather than performance:
----------------------------8< cut here >8------------------------------
#!/usr/bin/perl
use warnings;
use strict;

use constant BASE62DICT => join('', 0..9, 'A'..'Z', 'a'..'z');

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

foreach my $arg (@ARGV) {
print $arg, "\t", base62_to_num($arg), "\n";
}
----------------------------8< cut here >8------------------------------

Taking an id from your message: 1RgfJa-0004Tx-OG
% ./b62.pl 1RgfJa 0004Tx OG
1RgfJa 1325262498
0004Tx 17233
OG 1504
% time_render 1325262498
2011-12-30T11:28:18 -0500 (Fri Dec 30 11:28:18 2011 EST)

That looks to me like it's returning sane results.

My Perl is rusty, I've been mostly in Python for the past few years.
I'm sure there are more performant perlgolf hacks possible. I don't
care. :)
--
https://twitter.com/syscomet

--
## List details at https://lists.exim.org/mailman/listinfo/exim-users
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/


exim-users at spodhuis

Dec 31, 2011, 1:39 PM

Post #3 of 5 (528 views)
Permalink
Re: Base62 to Decimal [In reply to]

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

--
## List details at https://lists.exim.org/mailman/listinfo/exim-users
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/


richard at highwayman

Dec 31, 2011, 2:22 PM

Post #4 of 5 (530 views)
Permalink
Re: Base62 to Decimal [In reply to]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


http://search.cpan.org/~zefram/Data-ID-Exim-0.007/lib/Data/ID/Exim.pm

- --
richard @ highwayman . com "Nothing seems the same
Still you never see the change from day to day
And no-one notices the customs slip away"

-----BEGIN PGP SIGNATURE-----
Version: PGPsdk version 1.7.1

iQA/AwUBTv+LQ+INNVchEYfiEQKQ0wCeIsP1W7MElAWYSRa3E2WsQnFw7HsAn333
3S4Jiv69UyYhvN3BM8r+HKy1
=hpVZ
-----END PGP SIGNATURE-----

--
## List details at https://lists.exim.org/mailman/listinfo/exim-users
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/


exim-users at spodhuis

Dec 31, 2011, 5:32 PM

Post #5 of 5 (527 views)
Permalink
Re: Base62 to Decimal [In reply to]

On 2011-12-31 at 22:22 +0000, Richard Clayton wrote:
> http://search.cpan.org/~zefram/Data-ID-Exim-0.007/lib/Data/ID/Exim.pm

Thanks, I knew I'd heard of something, should have known Andrew would
have written it.

I'll drop him a note to lack the MacOS portability problem.
--
https://twitter.com/syscomet

--
## List details at https://lists.exim.org/mailman/listinfo/exim-users
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/

exim users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.