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

◇◆丶佛笑我妖孽 提交于 2019-12-22 09:20:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!