Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How do I determine encoding format of a file ?

by donno20 (Sexton)
on May 09, 2003 at 01:53 UTC ( [id://256731]=note: print w/replies, xml ) Need Help??


in reply to How do I determine encoding format of a file ?

Read the first two bytes of the file. Corresponding encoding and hex codes are as follow:
unicode Little Endian = "\xFF\xFE"
unicode Big Endian = "\xFE\xFF"
utf8 = "\xEF\xBB"
ASCII = straight to content
  • Comment on Re: How do I determine encoding format of a file ?

Replies are listed 'Best First'.
Re: Answer: How do I determine encoding format of a file ?
by graff (Chancellor) on May 09, 2003 at 05:05 UTC
    Maybe in a perfect world... Certainly, there's a reasonable chance that UTF-16(BE|LE) really will start with the "byte-order-mark" (BOM, \x{FEFF}), but oh, so many people are not so reasonable. I've seen UTF16 files created by Microsoft tools that were little-endian (of course) but had no BOM.

    As for utf8 files, um, where did this information about "\xEF\xBB" come from? I've never seen a file that starts like that (and I would have thought that any proper utf8 mechanism would barf given this sort of byte sequence -- an initial "\xEF" would dictate the start of a 3-byte character, but you don't indicate a third byte). If you mean \x{EFBB} (expressed as UTF-16), this would be three octets when converted to utf8: \xEE\xBE\xBB).

    Regarding the notion of ASCII data, many people don't realize that ASCII files are simply a proper subset of utf8 files -- this was, I think, one of the design goals for utf8. (This adds to my doubts about "\xEF\xBB": this sequence isn't supposed to be in an ASCII file, yet an ASCII file is supposed to work as a utf8 file.)

    I think the question, though vaguely stated, may have been more concerned with distinguishing, say, the different flavors of ISO-8859 (which is impractical without knowledge of the language being used in the text, or at least some well-trained n-gram models for various languages), or CP12* vs. Mac* vs. 8859-* vs. euc-*, etc, etc (somewhat less speculative, but still not always simple or deterministic without modeling).

    Perhaps more clarification is needed about the scope of the question. ("raw" text files? HTML/XML files? files that are simply "some form of unicode, but I don't know which"?)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://256731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found