Skip to content

Instantly share code, notes, and snippets.

@windows98SE
Created April 9, 2014 09:58
Show Gist options
  • Save windows98SE/10249531 to your computer and use it in GitHub Desktop.
Save windows98SE/10249531 to your computer and use it in GitHub Desktop.
[perl] hexdump
#!/usr/local/bin/perl
sub hexdump{
my $data = $_[0];
my $len = length($data);
#hex, content
my $buf_hex = uc(unpack('H*', $data));
$buf_hex =~ s/\G(..)/$1 /g;
my $buf_content = $data;
$buf_content =~ s/[^a-z0-9\\|,.<>;:'\@[{\]}#`!"\$%^&*()_+=~?\/ -]/./gi;
my $output = "\e[1;31m#0xADDRESS (00000) : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F : ---DATA---\e[0m\n";
for(my $offset=0,my $addr_start=0;$addr_start<$len;$offset+=16,$addr_start+=16){
my $hex = '';
my $content = '';
for(my $i=0;$i<16;$i++){
my $hex_tmp = substr($buf_hex, ($offset+$i)*3, 2);
$hex .= ($hex_tmp)?sprintf("%s ", $hex_tmp):" ";
$content .= sprintf("%s",substr($buf_content, $offset+$i, 1));
}
$output .= sprintf("0x%08X (%05d) : ", $addr_start, $addr_start)."$hex: $content\n";
}
return $output;
}
print hexdump($ARGV[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment