Skip to content

Instantly share code, notes, and snippets.

@wallneradam
Created February 7, 2018 12:38
Show Gist options
  • Save wallneradam/745929e423f468d65fae3167f69b4ead to your computer and use it in GitHub Desktop.
Save wallneradam/745929e423f468d65fae3167f69b4ead to your computer and use it in GitHub Desktop.
PHP Hex Dump
<?php
/**
* Print binary data as hex string
* @param string $data
* @param string $newline
* @param int $width
*/
function hexdump($data, $newline = "\n", $width = 16) {
static $from = '';
static $to = '';
static $pad = '.'; # padding for non-visible characters
if ($from === '') {
for ($i = 0; $i <= 0xFF; $i++) {
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
}
$hex = str_split(bin2hex($data), $width * 2);
$chars = str_split(strtr($data, $from, $to), $width);
$offset = 0;
foreach ($hex as $i => $line) {
echo sprintf('%06X', $offset) . ' : ' . str_pad(implode(' ', str_split($line, 2)), $width * 3) . ' ' . $chars[$i] . $newline;
$offset += $width;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment