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;
clock_crazyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
You've told us what you want, but haven't said how your script fails to meet your needs.

I do see a problem with this section:
foreach(split//,$buff)
       {
             if ($_ =~ /8..91/)

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?
0
ozoCommented:
/[\10-\133]/ should be equivalent to what you may be thinking /8..91/ means, although
/[ -~]/ or /[[:print:]]/ or /[[:graph:]]/ may better suit your purpose
0
clock_crazyAuthor Commented:
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";
0
ozoCommented:
I would not suggest using [ -~] in place of if ($_ =~ /[-~]/)
although you could use if( /[ -~]/ ) in place of if ($_ =~ /[-~]/)
I could suggest printf("%s ", $_); in place of printf("%s ", chr($_));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clock_crazyAuthor Commented:
thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.