Trag has asked for the wisdom of the Perl Monks concerning the following question:

I wrote these scripts a while ago and am only just coming back to them. They used to operate as a pair but I'm trying to use Binhex on its own. the problem is that Binhex will not properly put back together the hex file. Hexbin will (although but if I use it on a .wav file, the file has a lot of static in it. If I use Binhex to recompile it, Windows media player will not open the file. Anyone know what I'm doing wrong? (Also, I'm going to look into this myself, but if anyone knows how to split the txt files into bytes, please tell me).

Thanks, here's the code:

Binhex:
#!/usr/bin/perl use warnings; use strict; my $hexbin = shift; my $filename = shift; my $savefile = shift; open (INPUT,"$filename") || die "Couldn't open file: $!"; open (OUTPUT,">$savefile") || die "Unable to save: $!"; if ($hexbin eq "-h"||"-H") { binhex(); } elsif ($hexbin eq "-b"||"-B") { hexbin(); } close INPUT; close OUTPUT; exit 0; sub binhex { binmode INPUT; while (<INPUT>) { print OUTPUT unpack ('H*', $_), "\n"; } } sub hexbin { while (<INPUT>) { print OUTPUT pack ('H*', $_), "\n"; } }


Hexbin:
#!/usr/bin/perl use warnings; use strict; my $filename = shift; my $savefile = shift; open (HEX,"$filename") || die "Couldn't open file: $!"; open (SAVEFILE,">$savefile") || die "Unable to save: $!"; while (<HEX>) { print SAVEFILE pack ('H*', $_), "\n"; } close HEX; close SAVEFILE;


*********************************************
print "Just another iconoclastic Perl hacker";

Replies are listed 'Best First'.
Re: Hex editor
by japhy (Canon) on May 13, 2004 at 15:48 UTC
    The problem is hexbin() is never called, only binhex() is:
    if ($hexbin eq "-h"||"-H") { binhex(); } elsif ($hexbin eq "-b"||"-B") { hexbin(); }
    You're doing comparisons incorrectly. You can't say $x eq "Y" or "Z", you have to say $x eq "Y" or $x eq "Z". Because you're doing it this way, binhex() is ALWAYS called, because even if $hexbin is not "-h", the string "-H" is always true.
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
Re: Hex editor
by Sandy (Curate) on May 13, 2004 at 15:19 UTC
    Just a stab in the dark here, but

    your 'binhex' program reads in a binary file, and appends a newline at the end of each conversion (ok, if that's what you want to do), but when it reads the hexfile to convert it back to binary, not only does it not 'chomp' the newline that you added, it also adds another newline.

      Ah, Thanks. I'd vote you up, but I used all my votes today.

      Update: No luck, I got rid of the newline, but media player still won't open the wav file.

      *********************************************
      print "Just another iconoclastic Perl hacker";
        Are the number of bytes in your binary files the same (before and after)?. If not, keep checking those newlines.
Re: Hex editor
by graff (Chancellor) on May 14, 2004 at 02:56 UTC
    Japhy hit the nail on the head, so let me just offer some stylistic suggestions for that first script:
    #... my $Usage = "Usage: $0 {-b|-h} input.file output.file\n"; my ( $outType, $inpFile, $outFile ) = @ARGV; open (INP,"<$inpFile") || die "Can't read $inpFile: $!"; open (OUT,">$outFile") || die "Can't write $outFile: $!"; if ($outType =~ /^-h$/i) { binhex(); } elsif ($outType =~ /^-b/i) { hexbin(); } else { die $Usage; } #...
    If you decide to add more options later, you should look into GetOpt::Std or GetOpt::Long.