Created
December 6, 2018 15:11
-
-
Save esobchenko/e8ee43b4d14d54988e4707c28bc060f1 to your computer and use it in GitHub Desktop.
perl encode decode utf8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Encode qw/encode decode _utf8_on/; | |
use utf8; | |
binmode(STDOUT,':utf8'); # suppress "widecharacter in print" warnings | |
my $foo = "it's ascii"; | |
my $bar = "это юникод"; | |
my @a = ($foo, $bar); | |
for my $s (@a) { | |
printf "raw: is_utf8('$s') == %d\n", utf8::is_utf8($s); | |
my $e = encode("utf8", $s); | |
print "encoded octets = "; | |
print "0x$_ " for unpack "(H2)*", $e; | |
print "\n"; | |
my $d = decode("utf8", $e); | |
printf "decoded: is_utf8('$d') == %d\n", utf8::is_utf8($d); | |
print "decoded octets = $d\n"; | |
print "-" x 50; | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment