Thank you both for the response! To elaborate on my question, let's assume that the input file contains a binary number of:
"0101_1111_1111_0000_0111_0000_1111_1010"
Output should be:
"1_0101_1111_1111_0000_0111_0000_1111_1010"
This is purely binary input. I only added the "_" for readability.
I understand that we cannot write a file on non byte boundaries. So if I would always have 7 extra bits when this is written to the file. This would mean that my output file will look like:
"0000_0001_0101_1111_1111_0000_0111_0000_1111_1010"
When I run the code below, I get an output of:
"0000_0001_0000_0000_0000_0000_0000_0000_0000_0000"
I think I am still misusing the pack command. What am I missing?
Code below:
#!/usr/bin/perl
my $inputfile = $ARGV[0];
my $outputfile = $ARGV[1];
my $buff = "";
my $mybuff = "";
open (PARSER_FILE, $inputfile)
or die "Couldn't open $inputfile for reading";
binmode(PARSER_FILE);
open (OUT_FILE, ">", $outputfile)
or die "Couldn't open $outputfile for writing";
binmode(OUT_FILE);
read(PARSER_FILE, $buff, 4);
$outbuff = unpack("b32", $buff);
syswrite OUT_FILE, pack("b33", 1, $outbuff);
close(OUT_FILE);
close(PARSER_FILE)
-
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.