You should be able to do it by looking at the first two bytes to detect UCS2/UTF-16;

A UCS2 file would normally have a Byte Order Marker (BOM) Which is *either* 0xFF 0xFE or 0xFE 0xFF (depending on the endian-ness of your processor.

Now you fall into the realm of multibyte. In the parsers I've designed, where there is no other meta data (like an XML prolog, or some encoding information from HTTP) is to firstly attempt to parse as UTF8 (it's dead handy that the conversion is relatively trival) -- if it fails (so it finds an invalid UTF-8 character sequence) I fall back to the current locale.

* Actually Windows boxen use UCS2, not UTF-16; Strictly speaking, UTF-16 is a superset of this, and the UTF-16 is binary backwardly compatible with UCS2.


In reply to Re: Re: Re: Reading and writing to unicode files by RatArsed
in thread Reading and writing to unicode files by cbingel

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.