I'm reading an xml file in utf8 format. I want to strip out the xml and convert the data to iso-8859-2 octal codes for any letter not in the standard 7 digit ascii letters. So far it looks like my code works 99% of the time. But for some characters like a dash '-', it is converting it into a question mark '?'. Does this mean that it could not encode from a unicode - to the standard ascii - ?

Here is some code snippet:

use Encode; sub convertData { my $tempString = encode("iso-8859-2",shift); my $newString = ""; for ( my $l=length( $tempString), my $i = 0; $i < $l; $i++ ) { my $chr = substr( $tempString, $i, 1 ); my $ascii = ord( $chr ); if ( ( $ascii < 32 ) or ( $ascii > 126 ) ) { $newString .= sprintf("\\%03o", $ascii); } else { $newString .= $chr; } } return $newString; }

Also, I am fairly new to perl and encoding, so let me know if I am doing something that is not safe or totally correct.


In reply to convert utf8 to iso-8859-2 to octal code by sdperl

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.