0

我正在尝试从 2 个 HEX 对生成一个 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);

这是我要生成的字符:http ://www.fileformat.info/info/unicode/char/00e9/index.htm

感谢您的任何提示!

4

3 回答 3

3
use Encode;

my $byte1 = "C3";
my $byte2 = "A9";
my $bytes = chr(hex($byte1)) . chr(hex($byte2));
print decode_utf8($bytes);
于 2013-03-06T02:01:10.543 回答
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;
于 2013-03-06T02:23:40.447 回答
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);
于 2013-03-06T02:08:37.763 回答