How can I create a Unicode character from its bytes when they are stored in different variables in Perl?
hex, perl, unicode
Solution
Instead of
my $encoded = encode('utf8', "\x{${byte1}${byte2}}\x{${byte3}}");
You can use
my $encoded = encode('utf8', chr(hex($byte1 . $byte2)) . chr(hex($byte3)));
`hex()` converts from hexadecimal, and `chr()` returns the unicode character for a given code point.
[Edit:]
Not related to your question, but I noticed you mix `utf-8` and `utf8` in your program. I don't know if this is a typo, but you should be a ware that these are not the same things in Perl: `utf-8` (with hyphen, case insensitive) is what the UTF-8 standard says, whereas `utf8` (no hyphen, also case insensitive) is Perls internal encoding, which is more loosely defined (it allows codepoints that are not valid unicode codepoints). In general, you should stick to `utf-8` (perlunifaq has the details).
Problem
I am trying to Convert hex representations of Unicode characters to the characters they represent. The following example works fine: ``` #!/usr/bin/perl use Encode qw( encode decode ); binmode(STDOUT, ':encoding(utf-8)'); my $encoded = encode('utf8', "\x{e382}\x{af}"); eval { $encoded = decode('utf8', $encoded, Encode::FB_CROAK); 1 } or print("coaked\n"); print "$encoded\n"; ``` However the hex digits are stored in 3 variables. So if i replace the encode line with this: ``` my $encoded = encode('utf8', "\x{${byte1}${byte2}}\x{${byte3}}"); ``` where ``` my $byte1 = "e3"; my $byte2 = "82"; my $byte3 = "af"; ``` It fails as it tries to evaluate the \x immediately and sees the $ sign and { as characters. Does anyone know how to get around this.