Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

First of all: you can't have a "Unicode" file.

You can have a file containing Unicode code-points encoded in one of the transformation formats defined by the Unicode standard, such as UTF-8 or UTF-16.

So the question becomes:

I have a byte-stream. Is it a valid (ISO-8859-1|UTF-8|UTF-16)-encoded representation of some text?

This can be answered, since none of those encodings defines a meaning for each and every byte-sequence. But this is quite possibly not the answer you're looking for.

The way I see it, it's easier to check if your byte-stream contains something you know not to be text, using something like file(2) or File::MMagic as already suggested.

Doing it the other way ("is it a valid encoded form") gives you a lot of "this is text" when, in fact, it is nothing intelligible.

You could try to decode it and then do some heuristics to see if looks like text (ex. a lot of letters from the same script/writing system in a row, or something of the sort), but I think it's more trouble than it's worth.

-- 
        dakkar - Mobilis in mobile

Most of my code is tested...

Perl is strongly typed, it just has very few types (Dan)


In reply to Re: How can I tell if a string contains binary data or plain-old text? by dakkar
in thread How can I tell if a string contains binary data or plain-old text? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found