Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Converting utf-8 to base64 and back

by LanX (Saint)
on Jan 22, 2017 at 01:38 UTC ( [id://1180105]=note: print w/replies, xml ) Need Help??


in reply to Converting utf-8 to base64 and back

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1180105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found