Here a more concise demonstration.

(please note that PM has problems displaying wide-characters in code blocks, e.g. "ř" stands for "ř")

use strict; use warnings; use utf8 ; use Encode; use Data::Dump qw/dd pp/; use Devel::Peek; use MIME::Base64 ; use open IO => ':encoding(UTF-8)', ':std'; roundtrip($_) for 'Ä', 'ř'; sub roundtrip { my $orig = shift; warn "\n\n****** START\n"; peek( orig => $orig); my $encode_utf8 = Encode::encode_utf8($orig); peek (encode_utf8 =>$encode_utf8); my $encode_b64 = MIME::Base64::encode_base64($encode_utf8); peek(encode_b64=>$encode_b64); my $decode_b64 = MIME::Base64::decode_base64($encode_b64); peek (decode_b64=>$decode_b64); my $decode_utf8 = Encode::decode_utf8($decode_b64); peek (decode_utf8 => $decode_utf8); } sub peek { my ($name,$str) = @_; my (undef, $file, $line) = caller(0); my $pp = pp($str); warn "\n*** $name = '$str' = $pp at $file line $line\n"; Dump $_[1]; }

****** START *** orig = 'Ä' = "\xC4" at c:/tmp/b64_utf8_2.pl line 21 SV = PV(0x36c9d8) at 0x25c8308 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) PV = 0x367478 "\303\204"\0 [UTF8 "\x{c4}"] CUR = 2 LEN = 16 *** encode_utf8 = 'Ä' = "\xC3\x84" at c:/tmp/b64_utf8_2.pl line +25 SV = PV(0x2607c48) at 0x25c8110 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x28320c8 "\303\204"\0 CUR = 2 LEN = 16 *** encode_b64 = 'w4Q= ' = "w4Q=\n" at c:/tmp/b64_utf8_2.pl line 29 SV = PV(0x2607c58) at 0x25c80e0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2832188 "w4Q=\n"\0 CUR = 5 LEN = 16 *** decode_b64 = 'Ä' = "\xC3\x84" at c:/tmp/b64_utf8_2.pl line 3 +2 SV = PV(0x2607c38) at 0x25c7f48 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2832398 "\303\204"\0 CUR = 2 LEN = 16 *** decode_utf8 = 'Ä' = "\xC4" at c:/tmp/b64_utf8_2.pl line 36 SV = PV(0x2607bf8) at 0x25c7f00 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) PV = 0x28323c8 "\303\204"\0 [UTF8 "\x{c4}"] CUR = 2 LEN = 16 ****** START *** orig = 'ř' = "\x{159}" at c:/tmp/b64_utf8_2.pl line 21 SV = PV(0x36c9d8) at 0x25c8308 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) PV = 0x367478 "\305\231"\0 [UTF8 "\x{159}"] CUR = 2 LEN = 16 *** encode_utf8 = 'Å™' = "\xC5\x99" at c:/tmp/b64_utf8_2.pl line +25 SV = PV(0x2607c48) at 0x25c8110 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x28324e8 "\305\231"\0 CUR = 2 LEN = 16 *** encode_b64 = 'xZk= ' = "xZk=\n" at c:/tmp/b64_utf8_2.pl line 29 SV = PV(0x2607c58) at 0x25c80e0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2832518 "xZk=\n"\0 CUR = 5 LEN = 16 *** decode_b64 = 'Å™' = "\xC5\x99" at c:/tmp/b64_utf8_2.pl line 3 +2 SV = PV(0x2607c38) at 0x25c7f48 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2832428 "\305\231"\0 CUR = 2 LEN = 16 *** decode_utf8 = 'ř' = "\x{159}" at c:/tmp/b64_utf8_2.pl line 36 SV = PV(0x2607bf8) at 0x25c7f00 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) PV = 0x28322a8 "\305\231"\0 [UTF8 "\x{159}"] CUR = 2 LEN = 16

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!


In reply to Re: Converting utf-8 to base64 and back by LanX
in thread Converting utf-8 to base64 and back by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.