Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Converting Binary Numbers into Binary File

by echoangel911 (Sexton)
on Sep 13, 2006 at 13:28 UTC ( [id://572737]=perlquestion: print w/replies, xml ) Need Help??

echoangel911 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Hi, I have a file full of text with binary numbers. ie
0010010010011001 1110101010100101 ...
This is a text file with binary numbers inside? How can I convert this into a pure binary file?

Replies are listed 'Best First'.
Re: Converting Binary Numbers into Binary File
by ikegami (Patriarch) on Sep 13, 2006 at 13:48 UTC
    binmode($fh_out); while (<$fh_in>) { chomp; print $fh_out pack('B*', $_); }

    Reference: pack

Re: Converting Binary Numbers into Binary File
by BrowserUk (Patriarch) on Sep 13, 2006 at 15:31 UTC

    A word of caution: Be sure you understand what the numbers are, and what is going to be reading that binary file.

    There are several ways of interpreting that asciized binary, and several ways that they could need to be packed depending upon the target platform. Here are a few interpretations:

    print unpack 'n', pack 'B*', $_ for qw[ 0010010010011001 1110101010100 +101 ];; 9369 60069 print unpack 'n', pack 'b*', $_ for qw[ 0010010010011001 1110101010100 +101 ];; 9369 22437 print unpack 'v', pack 'b*', $_ for qw[ 0010010010011001 1110101010100 +101 ];; 39204 42327 print unpack 'v', pack 'B*', $_ for qw[ 0010010010011001 1110101010100 +101 ];; 39204 42474

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Converting Binary Numbers into Binary File
by Fletch (Bishop) on Sep 13, 2006 at 13:48 UTC

    Read the text. Use pack. Write the packed data.

Re: Converting Binary Numbers into Binary File
by davido (Cardinal) on Sep 13, 2006 at 15:25 UTC

    This deserves a one-liner:

    perl -ne "$i++ || binmode(STDOUT);chomp;print pack(q/B*/,$_);" infile +>outfile

    This is untested, but ought to work fine. It might be golfed down a bit if you allow it to set binmode on each iteration instead of only on the first iteration, and if you incorporate the -p switch instead of -n (which in this case, I believe hampers the legibility of the code):

    perl -pe "binmode STDOUT;chomp;$_ = pack(q/B*/,$_);" infile >outfile

    My biggest complaint with the golfed version is that it calls binmode on each iteration, which is unnecessary.


    Dave

      BEGIN block, then -

      perl -pe 'BEGIN{binmode STDOUT}$_=pack"B*",-+-$_'

      - 38 chars :-)

      <update>oops, wrong - that one strips leading zeroes.. oh, wait...

      perl -pe 'BEGIN{binmode STDOUT}chomp;$_=pack"B*",$_'
      </update>

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        I'll beat your 41
        perl -pe 'BEGIN{binmode STDOUT}chomp;$_=pack"B*",$_'
        with 36
        perl -pe 'BEGIN{binmode STDOUT}$_=pack"B16",$_'

Re: Converting Binary Numbers into Binary File
by Anonymous Monk on Sep 15, 2006 at 10:49 UTC
    what kind of virus is this?
      it's verilog simulation data of signals on certain ports

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found