Does it need to be Hex?
Have you considered 'base64'??
(1) There are plenty of modules to do so
(2) It's a portable format
(3) It's a 30% growth in filesize rather than 100%
(ie, each 8 bits becomes 6 rather than 4)
(4) You can unpack the data with other utilities.
---------------- sample code -------------------
sub EncodeBase64
{
my $s = shift ;
my $r = '';
while( $s =~ /(.{1,45})/gs ){
chop( $r .= substr(pack("u",$1),1) );
}
my $pad=(3-length($s)%3)%3;
$r =~ tr|` -_|AA-Za-z0-9+/|;
$r=~s/.{$pad}$/"="x$pad/e if $pad;
$r=~s/(.{1,72})/$1\n/g;
$r;
}
sub DecodeBase64
{
my $d = shift;
$d =~ tr!A-Za-z0-9+/!!cd;
$d =~ s/=+$//;
$d =~ tr!A-Za-z0-9+/! -_!;
my $r = '';
while( $d =~ /(.{1,60})/gs ){
my $len = chr(32 + length($1)*3/4);
$r .= unpack("u", $len . $1 );
}
$r;
}
my $binary_data = "Hello\nThere..\b\b\n";
my $ascii = EncodeBase64( $binary_data );
print "ASCII = $ascii".$/;
my $binary = DecodeBase64( $ascii );
print "binary = $binary".$/;
-------------------- snip ------------------------------
Regards,
David le Blanc
--
Senior Technical Specialist
I d e n t i t y S o l u t i o n s
Level 1, 369 Camberwell Road, Melbourne, Vic 3124
Ph 03 9813 1388 Fax 03 9813 1688 Mobile 0417 595 550
Email [EMAIL PROTECTED]
> -----Original Message-----
> From: Joel [mailto:[EMAIL PROTECTED]
> Sent: Friday, 5 March 2004 10:57 AM
> To: [EMAIL PROTECTED]; John W. Krahn
> Subject: Re: perl hex editor
>
> Thanks, that works perfectly. Now I just have to figure out
> how to turn it
> back into a valid file. I tried using "pack 'b*'" but I had
> no luck. Any
> suggestions?
>
> Joel
>
> ----- Original Message -----
> From: "John W. Krahn" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, March 03, 2004 5:31 PM
> Subject: Re: perl hex editor
>
>
> > Joel wrote:
> > >
> > > I'm trying to write a program in perl that will take a
> binary file,
> convert
> > > it to hex, then save it to a text file. So far I'm not
> having any luck.
> The
> > > best I've been able to do is copy the file and open it in
> a text editor.
> > > Here is my code:
> > >
> > > #!/usr/bin/perl
> > >
> > > use warnings;
> > > use strict;
> > >
> > > print "Enter path:\n";
> > > my $filename = <>;
> > > chomp $filename;
> > > print "Save as what?\n";
> > > my $savefile=<>;
> > > chomp $savefile;
> > > open (BINARY, "$filename") || die "Couldn't open file: $!";
> > > binmode BINARY;
> > > my $x=BINARY;
> > > open (SAVE,">$savefile") || die "Unable to save: $!";
> > > print SAVE 0b"$x";
> > > close BINARY;
> > > close SAVE;
> > >
> > > Any ideas?
> >
> > Perhaps this is what you want:
> >
> > #!/usr/bin/perl
> > use warnings;
> > use strict;
> >
> > print 'Enter path: ';
> > chomp( my $filename = <STDIN> );
> >
> > print 'Save as what? ';
> > chomp( my $savefile = <STDIN> );
> >
> > open BINARY, '<', $filename or die "Couldn't open file: $!";
> > binmode BINARY;
> > open SAVE, '>', $savefile or die "Unable to save: $!";
> >
> > while ( <BINARY> ) {
> > print SAVE unpack( 'H*', $_ ), "\n";
> > }
> >
> > close BINARY;
> > close SAVE;
> >
> > __END__
> >
> >
> >
> > John
> > --
> > use Perl;
> > program
> > fulfillment
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > <http://learn.perl.org/> <http://learn.perl.org/first-response>
> >
> >
> >
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>