phrase(utf8_codes(Codes), UTF8)
The algorithm is a close copy of the C-algorithm used internally and defined in src/pl-utf8.c
NOTE: in many cases you can avoid this library and leave
encoding and decoding to I/O streams. If only part of the data
is to be encoded the encoding of a stream can be switched
temporary using set_stream(Stream, encoding(utf8))