Jump to content

Hex dump: Difference between revisions

4,019 bytes added ,  18 days ago
Added PHP
m (Promote to full task)
(Added PHP)
Line 846:
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |.. |
00000068
</pre>
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
// Output the contents of a file (or part of a file) in either hexadecimal or binary
 
function dump($filename, $mode, $offset=0, $length=null) {
$data = file_get_contents($filename, false, null, $offset, $length);
$c = '';
$h = '';
$a = $offset;
$output = '<pre>';
 
if ($mode == 'h') {
$linesize = 16;
for ($i = 0; $i < strlen($data); $i++) {
$x = substr($data, $i, 1);
$h .= bin2hex($x) . ' ';
if ($i % 8 == 7) {
$h .= ' ';
}
if (ord($x) >= 32 && ord($x) <= 126) {
$c .= $x;
}
else {
$c .= '.';
}
if ($i % $linesize == 15 || $i == strlen($data) - 1) {
$o = str_pad(dechex($a), 8, '0', STR_PAD_LEFT) . ' ';
$a += $linesize;
$h = $o . str_pad($h, 50, ' ', STR_PAD_RIGHT) . '|' . $c . '|';
$output .= $h . '<br>';
$c = '';
$h = '';
}
}
}
elseif ($mode == 'b') {
$linesize = 6;
for ($i = 0; $i < strlen($data); $i++) {
$x = substr($data, $i, 1);
$h .= str_pad(decbin(ord($x)), 8, '0', STR_PAD_LEFT) . ' ';
if (ord($x) >= 32 && ord($x) <= 126) {
$c .= $x;
}
else {
$c .= '.';
}
if ($i % $linesize == 5 || $i == strlen($data) - 1) {
$o = str_pad(dechex($a), 8, '0', STR_PAD_LEFT) . ' ';
$a += $linesize;
$h = $o . str_pad($h, 54, ' ', STR_PAD_RIGHT) . '|' . $c . '|';
$output .= $h . '<br>';
$c = '';
$h = '';
}
}
}
// final byte count
$output .=str_pad(dechex(strlen($data)), 8, '0', STR_PAD_LEFT) . ' ';
$output .= '</pre>';
return $output;
}
$x = dump("file1.txt", "h");
echo '<p>Hexadecimal dump of file' . $x . '</p>';
 
$x = dump("file1.txt", "h", 10, 20);
echo '<p>Hexadecimal dump of 20 bytes of file from offset 10' . $x . '</p>';
 
$x = dump("file1.txt", "b");
echo '<p>Binary dump of file1' . $x . '</p>';
?>
</syntaxhighlight>
 
{{out}}
Hexadecimal dump of file
<pre>
00000000 ff fe 52 00 6f 00 73 00 65 00 74 00 74 00 61 00 |..R.o.s.e.t.t.a.|
00000010 20 00 43 00 6f 00 64 00 65 00 20 00 69 00 73 00 | .C.o.d.e. .i.s.|
00000020 20 00 61 00 20 00 70 00 72 00 6f 00 67 00 72 00 | .a. .p.r.o.g.r.|
00000030 61 00 6d 00 6d 00 69 00 6e 00 67 00 20 00 63 00 |a.m.m.i.n.g. .c.|
00000040 68 00 72 00 65 00 73 00 74 00 6f 00 6d 00 61 00 |h.r.e.s.t.o.m.a.|
00000050 74 00 68 00 79 00 20 00 73 00 69 00 74 00 65 00 |t.h.y. .s.i.t.e.|
00000060 20 00 3d d8 00 de 2e 00 | .=.....|
00000068
</pre>
 
Hexadecimal dump of file from offset 10 for length of 20
<pre>
0000000a 74 00 74 00 61 00 20 00 43 00 6f 00 64 00 65 00 |t.t.a. .C.o.d.e.|
0000001a 20 00 69 00 | .i.|
00000014
</pre>
 
Binary dump of file
<pre>
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .|
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.|
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.|
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.|
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.|
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.|
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.|
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.|
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.|
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.|
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.|
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.|
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.|
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |..|
00000068
</pre>
4

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.