Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Unpack function. Hex an input file

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello all,

pretty new to unpack function in Perl. I am trying to take an input file read it and use unpack to create a HEX output of the entire file. The only problem is that when I compare the HEX values of the file I am reading with the output of unpack some of the value are not correct, for some reason some hex values are off.

Example:
if my in file viewed in a HEX viewer shows the first the 8 bytes as 19 00 1d 12 33 0d 0a 00

The output of the unpack are shown as
19 00 1d 12 33 65 44 32

I was wondering if someone could let me know if I am going at this the correct way or if I am missing something in my code that maybe converting certain bytes incorrectly.

Thanks,
Code:
#!/usr/bin/perl

use strict;
use warnings;

my($strInputFile) = $ARGV[0];

if ($#ARGV ne "0")
{
 print "\n";
 print "Usage:\n";
 print "bin_hex.pl filename\n";

 exit;
}

if (open(INPUTFILE, "< $strInputFile"))
{
 binmode(INPUTFILE);
 while (<INPUTFILE>)
 {
  my $strBytes = $_;
  print unpack("H32", $strBytes);
 }

 close(INPUTFILE);
}
else
{
 print "ERROR! Cannot open file $strInputFile\n";
}
 
I would probably tackle this kind of problem by having it read one byte at a time, as an integer, and converting it to hex.

i.e.

Code:
open (BIN, "file.bin");
binmode BIN;

my $buffer;
while (read(BIN,$buffer,1)) {
   my $hex = sprintf("%02h", unpack("C",$buffer));
   print "$hex";
}

close (BIN);
print "\n";

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Actually that'd be %02x on the sprintf line.

Here's an example:

Code:
[kirsle@firefly ~]$ cat hello.bin 
hello
world!
[kirsle@firefly ~]$ perl
open (BIN, "hello.bin");
binmode BIN;
my $buffer;
while (read(BIN,$buffer,1)) {
	my $hex = sprintf("%02x",unpack("C",$buffer));
	print "$hex ";
}
print "\n";
close (BIN);
__END__
68 65 6c 6c 6f 0a 77 6f 72 6c 64 21 0a 
[kirsle@firefly ~]$ hexdump -C hello.bin 
00000000  68 65 6c 6c 6f 0a 77 6f  72 6c 64 21 0a           |hello.world!.|
0000000d

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
This file will output a line of hex and then directly below it the line in ascii.

---------------------------
#! /usr/bin/perl

use warnings;
use strict;

open (my $file, '<', $ARGV[0]) or
die "Unable to open $ARGV[0]: $!";

binmode $file;


while(read ($file, my $buffer,32))
{
for my $char (split '', $buffer)
{
my $hex = unpack 'H*', $char;
print "$hex "
}
print "\n";

for my $char (split '', $buffer)
{
my $ord = ord($char);
print $ord > 31 && $ord < 128 ? $char : '.';
print " ";
}
print "\n";
print "\n";
}
-------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top