Our Blog

Ongoing observations by End Point people

Byte-swap an entire file using perl

By David Christensen · Friday, April 20, 2012

Tags: perl

I recently needed to byte-swap an input file, and came up with an easy way with perl:

$ perl -0777ne 'print pack(q{V*},unpack(q{N*},$_))' inputfile > outputfile

This will byte-swap 4-byte sequences.  If you need to byte-swap 2-byte sequences, you can just adjust the formats for pack/unpack to the lower-case version like so:

$ perl -0777ne 'print pack(q{v*},unpack(q{n*},$_))' inputfile > outputfile

(Of course there are more efficient ways to handle this, but for a quick and dirty job this may just be what you need.)

We use the -0777 option to ensure perl reads the input file in its entirety and doesn't affect newlines, etc.