Here's my attempt. There's two versions of the functions here. The first uses multiplication and division to implement something along the lines of what you say, where each possible six character string is mapped to a the integers 0 .. 2_565_726_408, encoded as 4 bytes.
The second deals with the first three bytes and the second three bytes separately, mapping each to an integer in the range 0 .. 50652, and encoding each to strings of length 2 bytes, 4 bytes altogether.
Although I haven't benchmarked them, my gut tells me that the second is faster. Whatsmore, the second can run within a "use integer" block, which allows Perl to use fast integer maths. The first will not run within "use integer" because it sometimes overflows.
#!/usr/bin/perl
use strict;
use warnings;
{
use bytes;
my %lookup;
my @reverse;
my $scale;
BEGIN
{
my $x = 0;
my @chars = (' ', 0..9, 'A'..'Z');
keys %lookup = 65_536; # preallocate hash buckets
for my $i (@chars)
{
for my $j (@chars)
{
for my $k (@chars)
{
$lookup{ $i.$j.$k } = $x;
$reverse[$x++] = $i.$j.$k;
}
}
}
$scale = $x;
}
# Functions using multiplication...
sub alphanum_to_bytes_M
{
my $head = $lookup{ substr($_[0], 0, 3) };
my $tail = $lookup{ substr($_[0], 3, 3) };
my $n = ($head * $scale) + $tail;
pack(N => $n)
}
sub bytes_to_alphanum_M
{
my $n = unpack(N => $_[0]);
my $head = int($n / $scale);
my $tail = $n - ($head * $scale);
$reverse[$head] . $reverse[$tail]
}
# Functions using bitshifting...
{
use integer;
sub alphanum_to_bytes_B
{
my $head = $lookup{ substr($_[0], 0, 3) };
my $tail = $lookup{ substr($_[0], 3, 3) };
pack(nn => $head, $tail)
}
sub bytes_to_alphanum_B
{
join q{}, @reverse[ unpack(nn => $_[0]) ]
}
}
# Function to pretty-print byte strings for display purposes...
sub show_bytes
{
my ($str) = @_;
sprintf
'bytes[%s]',
join q{ },
map { sprintf('%02x', ord(substr($str, $_, 1))) }
0 .. length($str)-1
}
}
my @lines = <DATA>;
print "MULTIPLICATION:\n";
foreach (@lines)
{
my $b;
chomp;
printf(
"'%s' => '%s' => '%s'\n",
$_,
show_bytes($b = alphanum_to_bytes_M($_)),
bytes_to_alphanum_M($b),
);
}
print "BIT SHIFTING:\n";
foreach (@lines)
{
my $b;
chomp;
printf(
"'%s' => '%s' => '%s'\n",
$_,
show_bytes($b = alphanum_to_bytes_B($_)),
bytes_to_alphanum_B($b),
);
}
__DATA__
0
1
A
Z
ABCDEF
ABCDEG
ZAAAAA
ZZZZZZ
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.