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

Alrighty, folks, I got a question for you. I'm writing a script that will need to take a string (as in quoted) of 1's and 0's and be able to write that to a file. I don't know how to do this, anyone able to help?

Seems easy, right? Well, I don't just want a text file full of 101010101010. I want to write to the file in binary mode, if you will. So the 1010110101 that the program gets in will be the actual 1's and 0's that make up the new file.

In case you were wondering, I know the format of a certain type of file and am trying to make a script to automate a portion of the editing/creation process. I would normally edit this file in a hex editor.

--Psi
print(pack("h*","e4f64702566756e60236c6f637560247f602265696e676021602075627c602861636b65627e2")."\n");

Replies are listed 'Best First'.
Re: Writing a string of bits to a file, binary-style
by Zaxo (Archbishop) on Jun 15, 2001 at 02:07 UTC

    Your sig has nearly all the answer:

    print OUTFILE, pack("B*",$string);

    After Compline,
    Zaxo

    update: Idiotic blunder repaired

Re: Writing a string of bits to a file, binary-style
by tachyon (Chancellor) on Jun 15, 2001 at 02:21 UTC

    Adrift in a *nix sea a lonely Win32 hacker shouts into the wind "On Win32 don't forget to use binmode."

    cheers

    tachyon

Re: Writing a string of bits to a file, binary-style
by btrott (Parson) on Jun 15, 2001 at 02:09 UTC
    You mean, you want to interpret your binary string as bits? Use pack:
    print FH pack 'B*', '101010101010';
    Will that do it for you?
Re: Writing a string of bits to a file, binary-style
by bikeNomad (Priest) on Jun 15, 2001 at 02:13 UTC
    You want pack, with the B for bitstring (MS bit first) and the count of * to use up all the characters in your string:
    my $bits = '1010110101'; print OUTFILE pack('B*', $bits);