Hey Kevin,
It looks like you've reinvented the uuencoding algorithm. (See
http://www.opengroup.org/onlinepubs/7908799/xcu/uuencode.html for a
man page, assuming your local uuencode(1) man page doesn't include the
algorithm.)
Fortunately for your efficiency and fugliness concerns, Perl's pack() and
unpack() already do uuencoding:
$encoded = pack("u*", $s);
$decoded = unpack("u*", $encoded);
if ( $decoded ne $s ) { die "oops!\n"; }
Note the pack/unpack functions simply pass through trailing newlines
(embedded newlines are converted). This strikes me as very odd --
it makes it hard to emulate the uuencode/uudecode binaries' behavior
-- but at least they're consistent within Perl.
JD
On Fri, 1 Feb 2002, kevin lyda wrote:
> ok, earlier i posted a question on encoding a string. i found an answer.
> the goal was to take a string, break it into 6 bit chunks, add 32 to each
> chunk, then spew out the resulting string. the main characteristic of
> the resulting string is that it would be printable ascii - ascii 32 to 96.
>> the solution i came up with is just plain fugly in my opinion, but it
> works and i've spent too much time on this one section of code, so i'm
> sticking with it. however if someone's bored they can fiddle it to make
> it pretty. :)
>> thanks for all the replies!
>> #!/usr/bin/perl
>> sub encode_string {
> my($s) = @_;
>> my($encoded, $x, $chunk);
> $encoded = "";
> while (length($s) > 0) {
> $chunk = unpack("b24", $s);
> $s = substr($s, 3);
> foreach $x (1...4){
> $encoded .= chr(ord(pack("b6", $chunk)) + 32);
> $chunk = substr($chunk, 6);
> }
> }
> return $encoded;
> }
>> sub decode_string {
> my($e) = @_;
>> my($decoded, $x, $chunk, $tchunk);
> $decoded = "";
> while (length($e) > 0) {
> $chunk = unpack("b32", $e);
> $e = substr($e, 4);
> $tchunk = "";
> foreach $x (1...4){
> $tchunk .= unpack("b6", chr(ord(pack("b8", $chunk)) - 32));
> $chunk = substr($chunk, 8);
> }
> $decoded .= pack("b24", $tchunk);
> }
> return $decoded;
> }
>> $s = shift;
> $s = "\0\0\0AAA".chr(255).chr(255).chr(255)."BBBabcdef";
> $s .= "\0" x ((length($s) % 3)? 3 - (length($s) % 3): 0);
> $e = &encode_string($s);
> $d = &decode_string($e);
>> print("original: '$s' (".length($s).")\n".
> "encoded: '$e'\n".
> "decoded: '$d' (".length($d).")\n");
>> kevin
>> --
>kevin at suberic.net buffy: come on, can't you put your foot down?!
> fork()'ed on 37058400 giles: it *is* down.
> meatspace place: orbit buffy: one of these days you're going to have to
>http://suberic.net/~kevin get a grown up car. --inca mummy girl
>
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!