Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to print the actual bytes of UTF-8 characters ?

by ikegami (Pope)
on Feb 07, 2014 at 21:06 UTC ( #1073917=note: print w/ replies, xml ) Need Help??


in reply to How to print the actual bytes of UTF-8 characters ?

Use builtin utf8::encode or core Encode::encode_utf8 to get the UTF-8 encoding.

use utf8; # Source code is encoded using UTF-8. use open ':std', ':locale'; # Decode inputs and encode inputs. use strict; use warnings; use feature qw( say ); my @chars; for my $char (qw( )) { my $cp = ord($char); # Or unpack 'C' my $utf8 = $char; utf8::encode($utf8); my @utf8 = unpack('C*', $utf8); push @chars, [ $char, $cp, $utf8, @utf8 ]; }

Then it's just a question of displaying correctly.

my $last = 0; for (@chars) { $last = $#$_ if $last < $#$_; } say join ' ', map { sprintf '%-8s', $_->[0] } +@chars; say join ' ', map { sprintf '%-8d', $_->[1] } +@chars; say join ' ', map { sprintf '%-8s', sprintf '%02x', $_->[1] } +@chars; say join ' ', map { sprintf '%08b', $_->[1] } +@chars; say join ' ', map { sprintf '%-8s', sprintf '%*v02x', ' ', $_->[2] } +@chars; for my $i (3..$last) { say join ' ', map { defined($_->[$i]) ? sprintf '%08b', $_->[$i] : + (' 'x8) } @chars; }

Notes:

  • The binary of the code point could take up to 21 characters, but only 8 are available.
  • The hex of the UTF-8 bytes could take up to 11 chars, but only 8 are available.


Comment on Re: How to print the actual bytes of UTF-8 characters ?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1073917]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (18)
As of 2014-09-18 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls