#!/usr/bin/perl # bin2hex - wandle Binaerdatei in Hexadezimaldarstellung um. # hex2bin - wandle Hexadezimaldarstellung in Binaerdatei um. # # Aufruf: bin2hex binaerdatei hexdatei # oder    hex2bin hexdatei binaerdatei # # use strict; use warnings; die "Aufruf: $0 infile outfile" unless @ARGV == 2; my ($infile, $outfile) = @ARGV; die "Eingabe und Ausgabe muessen verschieden sein" unless $infile ne $outfile; open (my $in, $infile) or die "kann $infile nicht oeffnen: $!"; open (my $out, '>', $outfile) or die "kann $outfile nicht oeffnen: $!"; if ($0 =~ /bin2hex$/) {    bin2hex($in, $out); } elsif ($0 =~ /hex2bin$/) {    hex2bin($in, $out); } else { die "Das Programm muss bin2hex oder hex2bin heissen!"; } close $in; close $out; ########## sub bin2hex {    my ($from, $to) = @_;    binmode $in;    $/ = \8;    # immer 8 Bytes einlesen    while (<$from>) {        print $to unpack("H*", $_), "\n";    } } sub hex2bin {    my ($from, $to) = @_;    binmode $out;    while (<$from>) {        chomp;  # Zeilenenden abschneiden        print $to pack ("H*", $_);    } }