Perl Program to Print Unicode From Hex Value

  • A+
Category:Languages

I am starting up with Perl and confused on how to render unicode characters given a hex string variable.

#!/usr/bin/perl use warnings;  foreach my $i (0..10000) {     my $hex = sprintf("%X", $i);     print("unicode of $i is /x{$hex}/n"); } print("/x{2620}/n"); print("/x{BEEF}/n"); 

Gives me the warning: Illegal hexadecimal digit '$' ignored at perl.pl line 9.

and no value prints for /x{$hex}

 


Both chr($num) and pack('C', $num) produce a string consisting of the single character with the specified value, just like "/x{XXXX}" does.

As such, you can use

print("unicode of $i is ".chr(hex($hex))."/n"); 

or just

print("unicode of $i is ".chr($i)."/n"); 

Note that your program makes no sense without

use open ':std', ':encoding(UTF-8)'; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: