$bufsize = length $buffer; # Make sure it's an integer multiple of 8 by reducing by any remainder $bufsize -= $bufsize % 8; #you could copy substr($buffer, 0, $bufsize) if you wanted/needed to syswrite (OUTFILE, $buffer, $bufsize);