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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."