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


In reply to Unicode UTF16 - UTF8 conversion by belden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.