http://www.perlmonks.org?node_id=1073917


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: