Perl is supposed to treat 0xFC as a single byte.
No it's not. There are actually basically two different encodings for the same character: As one byte "\xFC" (ISO-Latin-1 string), and as 2 UTF-8 bytes, "\xC3\xBC" (UTF-8 string). That's one of the most annoying things about Perl's "smart" automatic UTF-8 processing: that it occasionally does the wrong thing, i.e. not what you mean, and that you're left to pick up the pieces.

These strings are actually marked as different, internally: the latter will have the UTF-8 flag set, the former, not. Perl can turn the former into the latter by joining it with a UTF-8 marked string. I'm not totally convinced that is something to cheer at. But anyway: it may even be a zero length string.

$x = "\xFC"; # ISO-Latin-1: 1 byte $x .= pack 'U0'; # append a zero length UTF-8 string # which will turn the whole thing into UTF-8! # hex dump: local($\, $,) = ("\n", " "); print map { sprintf "%02X", $_ } unpack 'C*', $x;
Result:
C3 BC
Using tricks like these, one can force perl into submission, even without use bytes (works even for pre 5.8 perls):
$l1 = pack 'C0a*', $s; # copies the bytes out of $s and marks the r +esult as non-UTF-8 $u8 = pack 'U0a*', $s; # copies the bytes out of $s and marks the r +esult as UTF-8 -- even if the bytes don't form proper UTF-8 strings!

In reply to Re: Re: Re: Re: Re: Re: Re: Re: length in bytes of utf8 string by bart
in thread length in bytes of utf8 string by mrd

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.