I'm using Perl 5.6 on Solaris

Is this a valid sub to check if an UTF8 strings contains chars that can be validly translated to Latin-1?

sub validUTF8{ ($_)=@_; use utf8; my @a=split "",$_; foreach (@a) { # if (/[\0-\x{ff}]/ or /[\x{C0}-\x{C3}][\x{80}-\x{FF}]/){ + if (/[\0-\x{ff}]/ or /[\x{C2}-\x{C3}][\x{80}-\x{FF}]/){ } else { print STDERR "BAD:$_\n"; no utf8; return 0; } } no utf8; return 1; } #converts from utf8 to latin1
I don't think the following 2 subs take into account all the latin-1 chars which doing tr one way or the other It doesn't include Latin-1 chars that can be represented like /[\x{C0}-\x{C3}][\x{80}-\x{FF}]. How do we write these better to correctly do the translations?
sub utf8tolatin1 { ($_)=@_; use utf8; tr/\0-\x{ff}//UC; #change utf8 to Latin-1 no utf8; return $_; } #converts from latin1 to utf8 sub latin1toutf8 { ($_)=@_; use utf8; tr/\0-\0xff//CU; #change Latin-1 to utf8 no utf8; return $_; }

In reply to UTF8 to Latin-1 and back using Perl-6 by kavita

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.