从两个HEX对生成UTF-8字符

时间:2013-03-06 01:35:23

标签: perl utf-8

我正在尝试从2个HEX对生成UTF-8字符。 HEX对来自字符串。

此代码有效:

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);

以下是我要生成的角色:http://www.fileformat.info/info/unicode/char/00e9/index.htm

感谢您的任何提示!

3 个答案:

答案 0 :(得分:3)

use Encode;

my $byte1 = "C3";
my $byte2 = "A9";
my $bytes = chr(hex($byte1)) . chr(hex($byte2));
print decode_utf8($bytes);

答案 1 :(得分:3)

将字符串文字视为迷你语言。你无法做到

"\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;

答案 2 :(得分:1)

Aahh ysth打败了我:

#!/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);