Hi Shawn, Thanks for your reply. On Thu, 12 Jul 2012 14:50:16 -0400 Shawn H Corey <shawnhco...@gmail.com> wrote:
> On 12-07-12 02:08 PM, Manfred Lotz wrote: > > The following code works fine. However, I like to know how to > > retrieve the UTF-8 hex representation of $uchar which is x'e0a487'. > > This is the internal representation in Perl, so it should be > > possible to print it out. Is there any function or module I could > > use to do this? > > Well, this is the hard way: > From this I assume there is no direct function doing this. > #!/usr/bin/env perl > > use 5.010; > use strict; > use warnings; > > use utf8; > > binmode STDOUT, ':utf8'; > > # this is code point U+0907, its name is > # DEVANAGARI LETTER I > # its utf8 hex representation is x'e0a487' > my $uchar = 'इ'; > > # this is 1 > my $len = length $uchar; > > say "Length of $uchar is $len"; > > my $hex = sprintf '%04x', ord($uchar); > say "Unicode for $uchar is U+$hex"; > > my $bytes; > open my $fh, '<', \$uchar or die $!; > { > local $/; > $bytes = <$fh>; > } > close $fh; > print "Bytes of $uchar are 0x"; > printf '%02x', ord( $_ ) for split //, $bytes; > say ''; > > On the one hand I believe there must be a 'better' way. On the other hand I like the idea (didn't occur to me) to read from the string as if it were a file. Thanks again. -- Manfred -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/