nglenn has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to output utf8 characters which correspond to the U+nnnn codes I have. I extract the nnnn and try to print the corresponding character using chr(), but sometimes it doesn't turn out right. I know that all of my code points are Characters, but look at this:

$var = 4628; open $report, '>utf8','C:report.txt' or die; print $report chr($var)."\x{4628}";

This prints"ሔ䘨". The \x{} is correct and the chr() is not. Why are they different? And if chr() is not an option, is there a viable way to use a variable in the \x{}? It isn't possible to just put "\x{$foo}" and I have played with eval() for a while with no luck.

Thanks in advance!

Nate

Update:

Worked like a charm! Thanks everyone. Just needed to use chr(hex $foo) instead of chr($foo).

Replies are listed 'Best First'.
Re: Difference between \x{} and chr()
by ikegami (Patriarch) on Nov 08, 2010 at 04:20 UTC
    4628 decimal isn't 4628 hex. chr(0x4628)
Re: Difference between \x{} and chr()
by GrandFather (Saint) on Nov 08, 2010 at 04:35 UTC

    Try:

    my $var = 0x4628; open my $report, '>utf8','C:report.txt' or die; print $report chr($var)."\x{4628}";

    Prints:

    True laziness is hard work
Re: Difference between \x{} and chr()
by JavaFan (Canon) on Nov 08, 2010 at 10:16 UTC
    print $report chr(hex $var). "\x{4628}";