Perl 从两个十六进制对生成UTF-8字符

Perl 从两个十六进制对生成UTF-8字符,perl,utf-8,Perl,Utf 8,我正在尝试从2个十六进制对生成一个UTF-8字符。十六进制对来自字符串 此代码适用于: use Encode; my $bytes = "\xC3\xA9"; print decode_utf8($bytes); # Prints: é and is correct 此代码不起作用: use Encode; my $byte1 = "C3"; my $byte2 = "A9"; my $bytes = "\x$byte1\x$byte2"; print decode_utf8($byte

我正在尝试从2个十六进制对生成一个UTF-8字符。十六进制对来自字符串

此代码适用于:

use Encode;

my $bytes = "\xC3\xA9";
print decode_utf8($bytes);

# Prints: é and is correct
此代码不起作用:

use Encode;

my $byte1 = "C3";
my $byte2 = "A9";
my $bytes = "\x$byte1\x$byte2";
print decode_utf8($bytes);
以下是我试图生成的角色:

谢谢你的提示

Aahh ysth打败了我:

use Encode;

my $byte1 = "C3";
my $byte2 = "A9";
my $bytes = chr(hex($byte1)) . chr(hex($byte2));
print decode_utf8($bytes);
#!/usr/bin/env perl

use strict;
use warnings;

use Encode;
use utf8::all;

my $byte1 = "C3";
my $byte2 = "A9";
my $bytes = join '', map {chr hex} $byte1, $byte2;

print decode_utf8($bytes);

将字符串文字视为一种小型语言。你做不到

"\x$hex"
比你能做的还要多

my $for = 'for';
$for (1..4) { }
但是有很多方法可以做你想做的事

my $bytes = join '', map chr hex, @bytes_hex;
my $bytes = pack 'C*', map hex, @bytes_hex;
my $bytes = pack '(H*)*', @bytes_hex;

这个答案非常有效!我感谢你的帮助。ikegami的回答帮助我了解了十六进制值的情况。也感谢你的回答。这也是有效的。谢谢你的解释。“\xCA”部分使我认为它是一个字符串,因为0xCA没有被引用。