i have a binary file splitted into 2 chunks, first part with all high bytes and the second part with all low bytes.
I need to combine the two chunks into one binary file like (eg. exactly the reverse of the splitting method solved in the thread # 130940)
I believe you both misunderstood. The task is to combine two binary files into a new binary file. The sample data in the OP are dumps of the actual data, not the actual data itself.
The od -like output that your solutions produce would require further processing to generate the desired binary output.
#!/usr/bin/perl -w
open HI, $ARGV[0] || die $!;
binmode HI;
open LO, $ARGV[1] || die $!;
binmode LO;
my $hidata;
my $lowdata;
while (read(HI, $hidata, 1)) {
read(LO, $lowdata, 1) || last;
printf "%c%c", ord($hidata),ord($lowdata);
}
close HI;
close LO;
Eg:
$ printf "\x0\x2\x4\x6\x8\xA" > Hi
$ printf "\x1\x3\x5\x7\x9\xB" > Lo
$ ./ans.pl Hi Lo > result
$ od -t d1 result
0000000 0 1 2 3 4 5 6 7 8 9 10 11
0000014
---------- Post updated at 06:29 PM ---------- Previous update was at 06:07 PM ----------
That's not quite correct. read -N (and read -n ) deal with characters. When dealing with binaries, you must work with bytes. Your solution is at the mercy of the current locale.
You need to explicitly specify a single-byte character locale, such as with LC_CTYPE=C (or LC_CTYPE=POSIX ).
Regards,
Alister
---------- Post updated at 06:51 PM ---------- Previous update was at 06:29 PM ----------
(1) od is using a signed format when it must be unsigned, and (2) the AWK command's output depends on locale. Both are fixed in this post's recommendation.