问题
Does anyone knows how to do this? Is this even possible?
I've read about decode and encode but since I'm not an expert I don't know if it will help.
回答1:
Of course, it is possible. If you have the byte array
my @bytes = (0xce, 0xb1, 0xce, 0xb2, 0xce, 0xb3);
you need to first combine those into a string of octets:
my $x = join '', map chr, @bytes;
Then, you can use utf8::decode to convert that to UTF-8 in place:
utf8::decode($x)
or die "Failed to decode UTF-8";
You can also use Encode::decode_utf8.
#!/usr/bin/env perl
use 5.020; # why not?!
use strict;
use warnings;
use Encode qw( decode_utf8 );
use open qw(:std :utf8);
my @bytes = (0xce, 0xb1, 0xce, 0xb2, 0xce, 0xb3);
my $x = join '', map chr, @bytes;
say "Using Encode::decode_utf8";
say decode_utf8($x);
utf8::decode($x)
or die "Failed to decode in place";
say "Using utf8::decode";
say $x;
Output:
C:\Temp> perl tt.pl Using Encode::decode_utf8 αβγ Using utf8::decode αβγ
Encode
allows you to convert among many character encodings. Its functions allow you to specify what happens in case the encoding/decoding operations fail whereas with utf8::decode
you are limited to explicitly checking success/failure.
来源:https://stackoverflow.com/questions/28788262/in-perl-how-can-i-convert-an-array-of-bytes-to-a-unicode-string