[Moscow.pm] Как закодировать UTF-8 в base64?

Alex Povolotsky tarkhil на over.ru
Чт Авг 11 06:54:50 PDT 2011


On 08/11/11 17:54, Сивиринов Иван wrote:
> Гм.. странно.
>
> Всегда прокатывало (utf-8 в том числе) что-то типа:
>
> use MIME::Base64 qw(encode_base64);
> sub _encode_subject {
>     my $class = shift;
>     my $enc   = shift;
>     my $subj  = shift;
>     $subj =~ s/\n/ /gm;
>     $subj = encode_base64($subj);
>     chomp $subj;
>     return sprintf('=?%s?B?%s?=', $enc, $subj);
> }
>
> но, боян же...
> или неправильно понял?

encode_base64( $bytes, $eol );

Encode data by calling the encode_base64() function. The first argument 
is the byte string to encode. The second argument is the line-ending 
sequence to use. It is optional and defaults to "\n". The returned 
encoded string is broken into lines of no more than 76 characters each 
and it will end with $eol unless it is empty. Pass an empty string as 
second argument if you do not want the encoded string to be broken into 
lines.

The function will croak with "Wide character in subroutine entry" if 
$bytes contains characters with code above 255. The base64 encoding is 
only defined for single-byte characters. Use the Encode module to select 
the byte encoding you want.

Alex


Подробная информация о списке рассылки Moscow-pm