Skip to content

Instantly share code, notes, and snippets.

@jinie
Created July 5, 2011 17:59
Show Gist options
  • Save jinie/1065429 to your computer and use it in GitHub Desktop.
Save jinie/1065429 to your computer and use it in GitHub Desktop.
Read binary file in perl and print hex output
#!/usr/bin/perl -w
use strict;
my $BLOCK_SIZE=1024;
my $fname = $ARGV[0];
open(F,"<$fname") or die("Unable to open file $fname, $!");
binmode(F);
my $buf;
my $ct=0;
while(read(F,$buf,$BLOCK_SIZE,$ct*$BLOCK_SIZE)){
foreach(split(//, $buf)){
printf("0x%02x ",ord($_));
}
print "\n";
$ct++;
}
close(F);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment