belden has asked for the wisdom of the Perl Monks concerning the following question:

Monks-

I am parsing the changes to Unihan in Unicode 3.1 Update-1.
I need to generate output in the form \0xE4\0xB8\0xAD

This is the UTF8 value for the Unicode character whose UTF16
value is U+4E2D. (In this case, the Chinese glyph "zhong1", middle.)

My code looks like this:

#!/usr/bin/perl my $utf16 = pack('U',hex(shift)); my $utf8; print("Before echo, length of $utf16: ", length $utf16, "\n"); chomp($utf16 = `echo $utf16`); print("After echo, ength of $utf16: ", length $utf16, "\n"); $utf8 .= sprintf("\\0x%x",ord($_)) foreach(split(/|/,$utf16)); print("$utf8\n"); exit;

What's nice is that I can use pack('U',$foo) to avoid having
to write a bit-shift sub to convert from UTF16 to UTF8.
It's a bit clumsy, though, in that it uses a system `echo`
to turn the packed UTF16 into an unpacked UTF16. (The two
length prints in the code.)

My question: how do I replace the `echo` with Perl without
reversing the UTF16 -> UTF8 conversion?

Thanks - Belden

Replies are listed 'Best First'.
Re: Unicode UTF16 - UTF8 conversion
by belden (Friar) on Sep 11, 2001 at 03:10 UTC
    Whoops; I hit 'submit' instead of 'preview' and missed a typo.

    Corrected form:
    " It's a bit clumsy, though, in that it uses a system `echo`
    " to turn the packed UTF8 into an unpacked UTF8.

    egg on my face - Belden