To whom: Using Perl and no other libraries, I am trying to
read in a binary file and I am getting a conversion
error. So, I sent the same file (word doc) through
Outlook Express to myself and looked over the source
code of the email and noticed that OE (in the multipart
section) had encoded the file different for some
characters. As a result the word doc is corrupt.
Any ideas why my routine gives me different results
than Outlook express? I've tried debugging it char-by-
char but that proved timely and useless.
(Unfortunately, my HOST doesn't have MIME library access. I
must do this in simple Perl. So the encode_base64
function I found on the web.)
MY CODE:
while (<$filename>) {
print encode_base64($_);
}
sub encode_base64 ($) {
my $res = "";
my $eol = "\n";
pos($_[0]) = 0;
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack("u", $1), 1);
chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9+/|;
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/"=" x $padding/e if $padding;
if (length $eol) {
$res =~ s/(.{1,76})/$1$eol/g;
}
$res;
}
Ever feel inches away from your destination and every
inch feels like a new beginning? Help. Peter.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.