In Perl, how can I convert an array of bytes to a Unicode string?

arrays, perl, utf-8

Solution

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.

Problem

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.

Original source