-
Notifications
You must be signed in to change notification settings - Fork 1
/
hexdump.php
66 lines (56 loc) · 1.33 KB
/
hexdump.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
if ( !defined( "HEXDUMP_INCLUDED" ) ) {
define( "HEXDUMP_INCLUDED", "1.0.0" );
/**
* Hexadecimal dumping functions
*
* Library: hexdump
* Provides: hexdump
* Depends:
*
* Helps you to find those pesky control characters
*
* I first wrote this code in PHP in Sep 2013. It echoed the output.
* Now I need it to be returned as a string, so rewriting as oik_hexdump().
* Quite a few people look for something similar. https://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php
*
*/
/**
* Returns the hex dump of the string
*/
function oik_hexdump( $string ) {
$hexdump = null;
$count = strlen( $string );
$hexdump .= $count;
$hexdump .= PHP_EOL;
$lineo = "";
$hexo = "";
for ( $i = 1; $i <= $count; $i ++ ) {
$ch = $string[ $i - 1 ];
if ( ctype_cntrl( $ch ) ) {
$lineo .= ".";
} else {
$lineo .= $ch;
}
$hexo .= bin2hex( $ch );
$hexo .= " ";
if ( 0 == $i % 20 ) {
$hexdump .= $lineo . " " . $hexo . PHP_EOL;
$lineo = "";
$hexo = "";
}
}
$hexdump .= substr( $lineo . str_repeat( ".", 20 ), 0, 20 );
$hexdump .= " ";
$hexdump .= $hexo;
$hexdump .= PHP_EOL;
return $hexdump;
}
/**
* Echoes the hex dump of a string
* @param $string
*/
function hexdump( $string ) {
echo oik_hexdump( $string );
}
}