Created
February 7, 2018 12:38
-
-
Save wallneradam/745929e423f468d65fae3167f69b4ead to your computer and use it in GitHub Desktop.
PHP Hex Dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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