[英]Generate a UTF-8 character from two HEX pairs
我正在嘗試從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);
這是我嘗試生成的字符: http : //www.fileformat.info/info/unicode/char/00e9/index.htm
感謝您的提示!
use Encode;
my $byte1 = "C3";
my $byte2 = "A9";
my $bytes = chr(hex($byte1)) . chr(hex($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;
亞哈(Yahth)打敗了我:
#!/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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.