in reply to Re^2: Convert binary file to ascii
in thread Convert binary file to ascii
Probably the easiest way would be to set $/ = \16 so that you read the file in 16 bytes chunks.
Ie. $/ = \16; while( my $line = <INFILE> ) { willresult in $line containing 16 bytes each time, which will give you your 8 values per output line.
Simplistically, that make the program something like:
#! perl -lw use strict; ## Note the -l above which makes print add newlines. open IN, '<:raw:perlio', $ARGV[0] or die $!; open OUT, '>', 'junk.out' or die $!; $/ = \16; ## read 16 bytes at a time; print join ',', map{ sprintf '0x%04x', $_ } unpack 'v*', $_ while <IN> +; close OUT; close IN;
If there was (still) some way to binmode *ARGV, it could be reduced to a one-liner:
perl -nle"BEGIN{$/=\16}print join',',map{sprintf'0x%04x',$_}unpack'v*' +,$_" binfile >outfile
but without binmode, that fails if the file contains a ^Z (control-Z; ascii 26) character.
Or you can go the other way and 'PBP-up' the above.
|
|---|