As described in UTF-8 and in Wikipedia, UTF-8 is a popular encoding of (multi-byte) Unicode code-points into eight-bit octets.

The goal of this task is to write a encoder that takes a unicode code-point (an integer representing a unicode character) and returns a sequence of 1-4 bytes representing that character in the UTF-8 encoding.

Then you have to write the corresponding decoder that takes a sequence of 1-4 UTF-8 encoded bytes and return the corresponding unicode character.

Demonstrate the functionality of your encoder and decoder on the following five characters:

Character   Name                                  Unicode    UTF-8 encoding (hex)
---------------------------------------------------------------------------------
A           LATIN CAPITAL LETTER A                U+0041     41
ö           LATIN SMALL LETTER O WITH DIAERESIS   U+00F6     C3 B6
Ж           CYRILLIC CAPITAL LETTER ZHE           U+0416     D0 96
€           EURO SIGN                             U+20AC     E2 82 AC
𝄞           MUSICAL SYMBOL G CLEF                 U+1D11E    F0 9D 84 9E

Provided below is a reference implementation in Common Lisp.

#!/usr/bin/perl
use strict;
use warnings;
use Unicode::UCD 'charinfo';         # getting the unicode name of the character
use utf8;                            # using non-ascii-characters in source code
binmode STDOUT, ":encoding(UTF-8)";  # printing non-ascii-characters to screen
 
my @chars = map {ord} qw/A ö Ж € 𝄞/; # @chars contains the unicode points
my $print_format = '%5s  %-35s';
printf "$print_format %8s  %s\n" , 'char', 'name', 'unicode', 'utf-8 encoding';
map{
	my $name = charinfo($_)->{'name'}; # get unicode name
	printf "$print_format  %06x  " , chr, lc $name, $_;
	my $utf8 = chr;                    # single char (using implicit $_)
	utf8::encode($utf8);               # inplace encoding into utf8 parts
	map{                               # for each utf8 char print ord
		printf " %x", ord;
	} split //, $utf8;
	print "\n";
} @chars;