Data::Hexdumper(3pm)
π Data::Hexdumper π
okay so bestie, you found yourself a Data::Hexdumper(3pm) page β it's from the User Contributed Perl Documentation collection, and it's about making binary data look cute instead of looking like a cursed wall of garbage. let's get into it π¦
β¨ NAME β¨
Data::Hexdumper β Make binary data human-readable π
π SYNOPSIS π
use Data::Hexdumper qw(hexdump);
print hexdump(
data => $data, # what to dump
# NB number_format is deprecated
number_format => 'S', # display as unsigned 'shorts'
start_position => 100, # start at this offset ...
end_position => 148 # ... and end at this offset
);
print hexdump(
"abcdefg",
{ output_format => '%4a : %C %S< %L> : %d' }
);
yes queen, two whole ways to call it. we love range π
πΈ DESCRIPTION πΈ
Data::Hexdumper gives you a simple way to format arbitrary binary data into a nice human-readable format β lowkey the same vibe as the Unix hexdump utility, but make it Perl.
It hands the programmer a considerable degree of flexibility in how the data gets formatted, with sensible defaults, because we're not monsters. The original author literally envisioned it being used by people wrestling alligators in the swamp of binary file formats, which is why it was written in the first place. iconic motivation tbh π
π¦ SUBROUTINES π¦
The following subroutines are exported by default β although bestie, that is deprecated and will be removed in some future version. Please pretend that you need to ask the module to export them to you. manifesting that energy β¨
β οΈ If you do assume that the module will always export them, then you may also assume that your code will break at some point after 1 Aug 2012. not a drill, fr fr π
π hexdump
Does everything. Takes a hash of parameters, one of which is mandatory, the rest having sensible defaults if not specified. Available parameters are:
dataβ A scalar containing the binary data we're interested in. This one is mandatory. no data, no dump, periodt.start_positionβ An integer telling us where indatato start dumping. Defaults to the beginning ofdata.end_positionβ An integer telling us where indatato stop dumping. Defaults to the end ofdata.number_formatβ This is deprecated. See 'INCOMPATIBLE CHANGES' below. If you use this, your data will be padded with NULLs to be an integer multiple of 16 bytes. You can expectnumber_formatto be removed at some point in 2014 or later. π«
A string specifying how to format the data. It can be any of the following, which β and you'll notice this immediately β have the same meanings as they do to perl's pack function:
Cβ unsigned charSβ unsigned 16-bit, native endiannessvorS<β unsigned 16-bit, little-endiannorS>β unsigned 16-bit, big-endianLβ unsigned 32-bit, native endiannessVorL<β unsigned 32-bit, little-endianNorL>β unsigned 32-bit, big-endianQβ unsigned 64-bit, native endiannessQ<β unsigned 64-bit, little-endianQ>β unsigned 64-bit, big-endian
Note that 64-bit formats are always available, even if your perl is only 32-bit. Similarly, using<and>on theSandLformats always works, even if you're using a pre 5.10.0 perl. That's because this code doesn't usepack(). no cap, it just does its own thing π
output_formatβ This is an alternative and much more flexible (but more complex) method of specifying the output format. Instead of specifying a single format for all your output, you can specify formats like:
%4a : %C %S %L> %Q : %d
which will, on each line, display first the address (consisting of '0x' and 4 hexadecimal digits, zero-padded if necessary), then a space, then a colon, then a single byte of data, then a space, then an unsigned 16-bit value in native endianness, then a space, then an unsigned 32-bit big-endian value, ... then a colon, a space, then the characters representing your 15 byte record. the way it just lays it all out π
You can use exactly the same characters and character sequences as are specified above for number_format, plus 'a' for the address, and 'd' for the data. To output a literal % character, use %% as is normal with formats β see sprintf for details. To output a literal < or > character where it may be confused with any of the {S,L,Q}{<,>} sequences, use %< or %>. So, for example, to output a 16-bit value in native endianness followed by <, use %S%<.
%a takes an optional base-ten number between the % and the a signifying the number of hexadecimal digits. This defaults to 4.
%{C,S,L,Q} also take an optional base-ten number between the % and the letter, signifying the number of repeats. These will be separated by spaces in the output. So '%4C' is equivalent to '%C %C %C %C'.
Anything else will get printed literally. This format will be repeated for as many lines as necessary. If the amount of data isn't enough to completely fill the last line, it will be padded with NULL bytes.
π¨ To specify bothnumber_formatandoutput_formatis a fatal error. pick a lane bestie π€
If neither are given, output_format defaults to:
' %4a : %16C : %d'
which is equivalent to the old-style:
number_format => 'C'
suppress_warningsβ Make this true if you want to suppress any warnings β such as that your data may have been padded with NULLs if it didn't exactly fit into an integer number of words, or if you do something that is deprecated. shhh era π€«space_as_spaceβ Make this true if you want spaces (ASCII character0x20) to be printed as spaces. Otherwise, spaces will be printed as full stops / periods (ASCII0x2E). choose your fighter π
Alternatively, you can supply the parameters as a scalar chunk of data followed by an optional hashref of the other options:
$results = hexdump($string);
$results = hexdump(
$string,
{ start_position => 100, end_position => 148 }
);
π SEE ALSO π
Data::DumperData::HexDumpif your needs are simpleperldoc -f unpackperldoc -f pack
go read those too, i'm not gatekeeping π«Ά
π INCOMPATIBLE CHANGES π
'number_format' is now implemented in terms of 'output_format'. Your data will be padded to a multiple of 16 bytes. Previously-silent code may now emit warnings. surprise! π
The mappings are:
'C' => ' %4a : %C %C %C %C %C %C %C %C %C %C %C %C %C %C %C %C : %d'
'S' => ' %4a : %S %S %S %S %S %S %S %S : %d'
'S<' => ' %4a : %S< %S< %S< %S< %S< %S< %S< %S< : %d'
'S>' => ' %4a : %S> %S> %S> %S> %S> %S> %S> %S> : %d'
'L' => ' %4a : %L %L %L %L : %d'
'L<' => ' %4a : %L< %L< %L< %L< : %d'
'L>' => ' %4a : %L> %L> %L> %L> : %d'
'Q' => ' %4a : %Q %Q : %d'
'Q<' => ' %4a : %Q< %Q< : %d'
'Q>' => ' %4a : %Q> %Q> : %d'
and of course:
'V' => 'L<'
'N' => 'L>'
'v' => 'S<'
'n' => 'S>'
π BUGS/LIMITATIONS π
- Behaviour of
%ais not defined if your file is too big. - Behaviour of
%NNais not defined ifNNis too big for your sprintf implementation to handle0x%0${NN}X.
undefined behaviour is giving "we don't talk about that" energy. tread carefully bestie π
π FEEDBACK π
I welcome constructive criticism and bug reports. Please report bugs either by email or via RT:
<http://rt.cpan.org/Public/Dist/Display.html?Name=Data-Hexdumper>
The best bug reports contain a test file that fails with the current code, and will pass once it has been fixed. The code repository is on Github:
<git://github.com/DrHyde/perl-modules-Data-Hexdumper.git>
yes, a failing test is the love language here. give them the receipts π§
π AUTHOR, COPYRIGHT and LICENCE π
Copyright 2001 - 2012 David Cantrell <david@cantrell.org.uk>
This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
π CONSPIRACY π
This module is also free-as-in-mason software. π β¨
πΈ THANKS TO ... πΈ
- MHX, for reporting a bug when dumping a single byte of data
- Stefan Siegl, for reporting a bug when dumping an ASCII 0
- Steffen Winkler, for inspiring me to use proper output formats
shoutout to the bug reporters, the real main characters fr fr π«Ά
okay bestie that's the whole page!! and just so you know where this version came from: it's perl v5.32.0, dated 2021-01-05, and it's Data::Hexdumper(3pm) β go forth and dump some bytes, you're gonna slay πππ¦
π¦ where this page comes from
- debian bookworm: libdata-hexdumper-perl 3.0001-1.1 Β· Artistic-1.0-Perl OR GPL-2.0-only
- debian trixie: libdata-hexdumper-perl 3.0001-1.1 Β· Artistic-1.0-Perl OR GPL-2.0-only
- ubuntu jammy: libdata-hexdumper-perl 3.0001-1.1 Β· Artistic-1.0-Perl OR GPL-2.0-only
- ubuntu noble: libdata-hexdumper-perl 3.0001-1.1 Β· Artistic-1.0-Perl OR GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms