Skip to content

Instantly share code, notes, and snippets.

@shahril96
Last active December 27, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahril96/7262960 to your computer and use it in GitHub Desktop.
Save shahril96/7262960 to your computer and use it in GitHub Desktop.
I was trying to make a PHP script that can output result same as HxD so I created this file.
<?php
hex_viewer('./test_file', 8);
function hex_viewer($File, $Length = 16) {
$handle = @fopen($File, 'r');
if(!$handle) {
return 0;
}
$contents = fread($handle, filesize($File));
fclose($handle);
foreach( str_split($contents, $Length) as $n => $line ) {
$N_Length = strtoupper(base_convert((($n) * $Length), 10, 16));
echo ' '.str_pad($N_Length, 8, '0', STR_PAD_LEFT);
$tmpStore = NULL;
for( $i = 0; $i < strlen($line); $i++ ) {
$toHex = base_convert(ord($line[$i]), 10, 16);
$toHex = (strlen($toHex) == 1)?'0'.$toHex:$toHex;
$Spaces = ($i < (strlen($line) - 1))?' ':'';
$tmpStore .= strtoupper($toHex).$Spaces;
}
if( $line < $Length ) {
$Cnt = $Length - strlen($line);
for( $i = 0; $i < $Cnt; $i++ ) {
$tmpStore .= ' 00';
}
}
$Str = preg_replace('/[\r|\t|\n]/', '.', $line);
echo ' '.$tmpStore.' '.$Str."\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment