Is $data binary or ascii?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Is $data binary or ascii?

    Say I have a variable $data that has file contents in it.
    Is there a way to tell if it is binary or ascii?

    if($data =~ ???) { print "It is binary weeee"; }
    elsif($data =~ ???) { print "It is ascii woo hoo"; }
    else { print "I do not know what it is, doh!"; }

    TIA

    Dan
    Dan Muey Guest

  2. Similar Questions and Discussions

    1. Webservices & binary data?
      Hi, We have a database that stores binary data such as word documents. Can these documents be delivered over Webservices by other applications...
    2. Freehand Binary Data
      When printing from my Freehand 9, I often get a message "Page contains EPS pictures which include binary data" What does this mean? I also have had...
    3. How can I force Illustrator to always print with ASCII data?
      I'm a student in an Info-Tech class, and now that we're done programming, we are working on graphic design (etc). The problem is, Illustrator...
    4. binary data
      Greets, Register globals are to OFF - however the files will not upload. Thanking all in advance. TR ............... <? if ($submit) {
    5. Binary vs ASCII - why is this an issue in CS?
      Don't know if this has to do with anything... I get that message in Quark 4 if ASCII is checked in the prining options box. I just check the...
  3. #2

    Default RE: Is $data binary or ascii?

    if -T ("looks like text file")
    If -B ("looks like binary file")

    if -T $data ....
    if -B $data ....

    HTH
    Paul
    -----Original Message-----
    From: Dan Muey [mailto:dmuey@infiniplex.com]
    Sent: Tuesday, September 09, 2003 11:57 AM
    To: [email]beginners@perl.org[/email]
    Subject: Is $data binary or ascii?


    Say I have a variable $data that has file contents in it.
    Is there a way to tell if it is binary or ascii?

    if($data =~ ???) { print "It is binary weeee"; }
    elsif($data =~ ???) { print "It is ascii woo hoo"; }
    else { print "I do not know what it is, doh!"; }

    TIA

    Dan

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]

    Paul Kraus Guest

  4. #3

    Default RE: Is $data binary or ascii?


    > if -T ("looks like text file")
    > If -B ("looks like binary file")
    >
    > if -T $data ....
    > if -B $data ....
    >
    > HTH
    Definitely! Thanks Paul.
    I never thought of using the file test operators on variables!
    Cool.

    Thanks!
    > Paul
    > -----Original Message-----
    > From: Dan Muey [mailto:dmuey@infiniplex.com]
    > Sent: Tuesday, September 09, 2003 11:57 AM
    > To: [email]beginners@perl.org[/email]
    > Subject: Is $data binary or ascii?
    >
    >
    > Say I have a variable $data that has file contents in it.
    > Is there a way to tell if it is binary or ascii?
    >
    > if($data =~ ???) { print "It is binary weeee"; }
    > elsif($data =~ ???) { print "It is ascii woo hoo"; }
    > else { print "I do not know what it is, doh!"; }
    >
    > TIA
    >
    > Dan
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    Dan Muey Guest

  5. #4

    Default RE: Is $data binary or ascii?

    On Sep 9, Dan Muey said:
    >> if -T $data ....
    >> if -B $data ....
    >
    >I never thought of using the file test operators on variables!
    I'm not so sure you can. But the docs say that -B looks for at least 30%
    of the characters to be control-chars or high-bit chars. So I'd suggest:

    sub is_binary_data {
    local $_ = shift;
    (tr/ -~//c / length) >= .3;
    }

    The tr/ -~//c takes the range of characters from space to tilde (32-126),
    inverts it (so characters 0-31 and 127-255), and counts how many of them
    are in $_. Then we divide that by the length of the string. If that's at
    least .3, that means at least 30% of the string is binary data.

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  6. #5

    Default RE: Is $data binary or ascii?

    > On Sep 9, Dan Muey said:
    >
    > >> if -T $data ....
    > >> if -B $data ....
    > >
    > >I never thought of using the file test operators on variables!
    >
    > I'm not so sure you can. But the docs say that -B looks for
    I was afraid of that. After trying it out I couldn't get any good results.
    > at least 30% of the characters to be control-chars or
    > high-bit chars. So I'd suggest:
    >
    >
    > sub is_binary_data {
    > local $_ = shift;
    > (tr/ -~//c / length) >= .3;
    > }
    >
    > The tr/ -~//c takes the range of characters from space to
    > tilde (32-126), inverts it (so characters 0-31 and 127-255),
    > and counts how many of them are in $_. Then we divide that
    > by the length of the string. If that's at least .3, that
    > means at least 30% of the string is binary data.
    >
    Excellent! Since the -B needed to be a file I was thinking about how to mimik the -B option.
    That is excellent, thanks for the explanation and info!

    Thanks Jeff!
    Dan
    > --
    > Jeff "japhy" Pinyan [email]japhy@pobox.com[/email]
    Dan Muey Guest

  7. #6

    Default Re: Is $data binary or ascii?

    Dan Muey wrote:
    >
    > Say I have a variable $data that has file contents in it.
    > Is there a way to tell if it is binary or ascii?
    >
    > if($data =~ ???) { print "It is binary weeee"; }
    > elsif($data =~ ???) { print "It is ascii woo hoo"; }
    > else { print "I do not know what it is, doh!"; }

    if ( $data =~ /[^[:ascii:]]/ ) {
    print "\$data contains non-ASCII characters\n";
    }
    else {
    print "\$data contains only ASCII characters\n";
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  8. #7

    Default RE: Is $data binary or ascii?

    > Dan Muey wrote:
    > >
    > > Say I have a variable $data that has file contents in it.
    > > Is there a way to tell if it is binary or ascii?
    > >
    > > if($data =~ ???) { print "It is binary weeee"; }
    > > elsif($data =~ ???) { print "It is ascii woo hoo"; }
    > > else { print "I do not know what it is, doh!"; }
    >
    >
    > if ( $data =~ /[^[:ascii:]]/ ) {
    > print "\$data contains non-ASCII characters\n";
    > }
    > else {
    > print "\$data contains only ASCII characters\n";
    > }
    Interesting John.
    Anyone know where is the [:ascii:] class/group/whatever you call it documented?
    I'd like to get more info on it to compare the other method mentioned earlier in the thread,
    {which works good on all my tests so far)

    Thanks

    Dan
    >
    >
    >
    > John
    Dan Muey Guest

  9. #8

    Default Re: Is $data binary or ascii?

    Dan Muey wrote:
    >
    > Anyone know where is the [:ascii:] class/group/whatever you call it documented?
    perldoc perlre


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  10. #9

    Default RE: Is $data binary or ascii?

    > Dan Muey wrote:
    > >
    > > Anyone know where is the [:ascii:] class/group/whatever you call it
    > > documented?
    >
    > perldoc perlre
    Thanks I'll have a look then!
    >
    >
    > John
    > --
    > use Perl;
    > program
    > fulfillment
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    Dan Muey Guest

  11. #10

    Default RE: Is $data binary or ascii?

    > > Dan Muey wrote:
    > > >
    > > > Anyone know where is the [:ascii:] class/group/whatever
    > you call it
    > > > documented?
    > >
    > > perldoc perlre
    >
    > Thanks I'll have a look then!
    Looks like it's a Posix thing added in 5.8.0 which is cool and can be written as
    [:^ascii:]
    Instead of [^[:ascii:]]

    Very cool, although it only works on my 5.8.0 and not 5.0
    (I don't have a 5.6 ish anyone know when the Posix support was added to regexes?)

    This could be usefull if I could do
    if($perlversion >= 5.8.0) { do the posix [:^ascii:] version }
    Else { do it the -B emulation way }

    Anyone know how to check the version without putting use 5.8.0; at the top?
    Dan
    >
    > >
    > >
    > > John
    > > --
    > > use Perl;
    > > program
    > > fulfillment
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    > >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    Dan Muey Guest

  12. #11

    Default Re: Is $data binary or ascii?

    On Tuesday, September 9, 2003, at 01:28 PM, Dan Muey wrote:
    > Looks like it's a Posix thing added in 5.8.0 which is cool and can be
    > written as
    > [:^ascii:]
    > Instead of [^[:ascii:]]
    >
    > Very cool, although it only works on my 5.8.0 and not 5.0
    > (I don't have a 5.6 ish anyone know when the Posix support was added
    > to regexes?)
    I don't know the version it was added in, but it works in Perl 5.6.0.

    James

    James Edward Gray II Guest

  13. #12

    Default RE: Is $data binary or ascii?

    > On Tuesday, September 9, 2003, at 01:28 PM, Dan Muey wrote:
    >
    > > Looks like it's a Posix thing added in 5.8.0 which is cool
    > and can be
    > > written as
    > > [:^ascii:]
    > > Instead of [^[:ascii:]]
    > >
    > > Very cool, although it only works on my 5.8.0 and not 5.0
    > > (I don't have a 5.6 ish anyone know when the Posix support was added
    > > to regexes?)
    >
    > I don't know the version it was added in, but it works in Perl 5.6.0.
    Yeah 5.6.0 had a lot of goodies like that added. It was probably then.
    Anybody else then, 5.6.0 going once going twice....
    >
    > James
    >
    >
    Dan Muey Guest

  14. #13

    Default Re: Is $data binary or ascii?

    James Edward Gray II wrote:
    >
    > On Tuesday, September 9, 2003, at 01:28 PM, Dan Muey wrote:
    >
    > > Looks like it's a Posix thing added in 5.8.0 which is cool and can be
    > > written as
    > > [:^ascii:]
    > > Instead of [^[:ascii:]]
    > >
    > > Very cool, although it only works on my 5.8.0 and not 5.0
    > > (I don't have a 5.6 ish anyone know when the Posix support was added
    > > to regexes?)
    >
    > I don't know the version it was added in, but it works in Perl 5.6.0.
    It was added to Perl 5.6.0.

    perldoc perldelta
    [snip]
    POSIX character class syntax [: :] supported

    For example to match alphabetic characters use
    /[[:alpha:]]/. See the perlre manpage for details.

    $ perl -v

    This is perl, v5.6.0 built for i586-linux




    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139