The problem is that you are ANDing and ORing numeric constants with the string value read from the file. If you ahd strict & warnings enabled, you would be getting an message telling you of the problem:
$s = 'AB'; print $s & 0xFC7F;; Argument "AB" isn't numeric in bitwise and (&) at ... 0
You either need to unpack the two bytes read from the file to an integer before performing your boolean math:
$s = unpack 'v', 'AB';; print pack 'v', $s & 0xFC7F;; A@
And then repack the result before writing to the output file.
Or, use string constants instead of numeric constants in your boolean math:
$s = 'AB';; print $s & "\x7F\xFC";; A@
But note how I had to reverse the order of the bytes in the string constant.
In both cases, you have to be aware of the byte order used by the system that produces the input file, and the byte order of the system that is running your code in order to ensure that you get the desired result.
In reply to Re: Question about binary file I/O
by BrowserUk
in thread Question about binary file I/O
by TheMartianGeek
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |