clock_crazy
asked on
binary to char / hexadecimal
I have a program, that prints hexadecimal if invoked without -c option,
If invoked with -c option, I want to determine if character can be printed with standard keyboard, if so, I want to print that key and move on to next character. If not, I want to print that character in hexadecimal character.
my.png is binary file.
can anyone comment
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '<', '/my.png' or die "Couldn't read file: $! \n";
binmode ($fh);
my ($buff,$var);
if ( @ARGV >= 1 && shift eq "-c" )
{
$var = shift;
while ( my $len = read($fh , $buff, 16))
{
foreach(split//,$buff)
{
if ($_ =~ /8..91/)
{
printf("%s ", chr($_));
}
else
{
printf("%02x ", ord($_));
}
}
print "\n";
}
}
else
{
while ( my $len = read($fh , $buff, 16 ))
{
foreach(split//,$buff)
{
printf("%02x ", ord($_));
}
print "\n";
}
}
close $fh;
If invoked with -c option, I want to determine if character can be printed with standard keyboard, if so, I want to print that key and move on to next character. If not, I want to print that character in hexadecimal character.
my.png is binary file.
can anyone comment
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '<', '/my.png' or die "Couldn't read file: $! \n";
binmode ($fh);
my ($buff,$var);
if ( @ARGV >= 1 && shift eq "-c" )
{
$var = shift;
while ( my $len = read($fh , $buff, 16))
{
foreach(split//,$buff)
{
if ($_ =~ /8..91/)
{
printf("%s ", chr($_));
}
else
{
printf("%02x ", ord($_));
}
}
print "\n";
}
}
else
{
while ( my $len = read($fh , $buff, 16 ))
{
foreach(split//,$buff)
{
printf("%02x ", ord($_));
}
print "\n";
}
}
close $fh;
/[\10-\133]/ should be equivalent to what you may be thinking /8..91/ means, although
/[ -~]/ or /[[:print:]]/ or /[[:graph:]]/ may better suit your purpose
/[ -~]/ or /[[:print:]]/ or /[[:graph:]]/ may better suit your purpose
ASKER
Thank you FishMonger & OZO for quick response
when i included [ -~] in place of if ($_ =~ /[-~]/) i got following error messages
Argument "-" isn't numeric in chr at line 22
Argument "~" isn't numeric in chr at line 22
also output i am expecting is ( listed below ) i.e if character can be printed with standard keyboard, i want to print it and if can not be printed via keyboard then print in hexadecimal.
89 P N G 0d 0a 1a 0a 00 00 00 0d I H D R 00 00 01 f9
00 00 01 9c 08 02 00 00 00 U ed N Z 00 00 00 09 p H Y
s 00 00 0e c4 00 00 0e c4 01 95 + 0e 1b 00 00 00 07 t I
M E 07 da 04 0d 11 13 1d g 96 0f f8 00 00 00 07 t E X
t A u t h o r 00 a9 ae cc H 00 00 00 0c t E X t
D e s c r i p t i o n 00 13 09 ! # 00 00 00 0a
also to answer Fishmonger, i was using earlier ( below script ) which i changed to simpler version.
print "$i:" . join (' ', map{unpack('H*',$_)} split //, $len) . "\n";
when i included [ -~] in place of if ($_ =~ /[-~]/) i got following error messages
Argument "-" isn't numeric in chr at line 22
Argument "~" isn't numeric in chr at line 22
also output i am expecting is ( listed below ) i.e if character can be printed with standard keyboard, i want to print it and if can not be printed via keyboard then print in hexadecimal.
89 P N G 0d 0a 1a 0a 00 00 00 0d I H D R 00 00 01 f9
00 00 01 9c 08 02 00 00 00 U ed N Z 00 00 00 09 p H Y
s 00 00 0e c4 00 00 0e c4 01 95 + 0e 1b 00 00 00 07 t I
M E 07 da 04 0d 11 13 1d g 96 0f f8 00 00 00 07 t E X
t A u t h o r 00 a9 ae cc H 00 00 00 0c t E X t
D e s c r i p t i o n 00 13 09 ! # 00 00 00 0a
also to answer Fishmonger, i was using earlier ( below script ) which i changed to simpler version.
print "$i:" . join (' ', map{unpack('H*',$_)} split //, $len) . "\n";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you
I do see a problem with this section:
Open in new window
Since you're splitting on each char, how would the regex match a 2 char string?Also, why are you assigning $len but never use that var?