Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

hexdump2bin: convert hexdump -C like output back to binary

by dmitri (Curate)
on Jun 19, 2013 at 02:02 UTC ( #1039708=CUFP: print w/ replies, xml ) Need Help??

#!/usr/bin/perl # Convert hexdump -C like output back to binary. Supports * lines, wh +ich # xxd -r does not (this is the reason for this script). Assumes good +input. use strict; use warnings; my ($off, $line, $asterisk); while (<>) { if (/^([[:xdigit:]]{2,}0)\s+((?:[[:xdigit:]]{2}\s+){1,16})/) { if (defined($asterisk)) { my $nlines = (hex($1) - hex($off)) / 16 - 1; print map { chr hex } split /\s+/, $line x $nlines; undef $asterisk; } print map { chr hex } split /\s+/, $2; $off = $1, $line = $2; } elsif (/^\*/) { $asterisk = 1; } else { last; } }

Comment on hexdump2bin: convert hexdump -C like output back to binary
Download Code
Re: hexdump2bin: convert hexdump -C like output back to binary
by Anonymous Monk on Oct 07, 2014 at 10:57 UTC
    When I try hexdump2bin.pl on a large 'hexdump -C' output with many '*' lines covering large offsets it fails "Out of memory!". I am not a perl expert. Can this be fixed?

      Most likely this line:

      print map { chr hex } split /\s+/, $line x $nlines;

      is problematic as it constructs a string of $nlines * length($nlines) bytes in memory before splitting and then printing it.

      The most likely approach to fix this would be to convert the statement to a loop:

      for my $repeat (1..$nlines) { print map { chr hex } split /\s+/, $line; }

      I haven't tested this change, so you're advised to compare the results of the changed file to the results without the change before trusting the output.

      Personally, I wonder how large your input hexdump and your output hexdump files are. Perl usually can handle strings up to 2GB without any problem, so likely your machine has less RAM than that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1039708]
Approved by bulk88
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-12-27 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls