The following function is a php hexadecimal instance that prints a string. The core function here is chr to obtain the binary value and convert it to the hexadecimal number. the code is as follows: & lt ;? Php * php printing
The following function is a php hexadecimal instance that prints a string. The core function here is chr to obtain the binary value and convert it to the hexadecimal value. The code is as follows:
-
- /*
- Php prints the hexadecimal data of the string.
- */
- Function hex_dump ($ data, $ newline = "n ")
- {
- Static $ from = '';
- Static $ to = '';
- Static $ width = 16; # number of bytes per line
- 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 ('% 6x', $ offset ). ':'. implode ('', str_split ($ line, 2 )). '['. $ chars [$ I]. ']'. $ newline;
- $ Offset + = $ width;
- }
- }
- $ Info = "this is a testx00x99hex_dump ";
- Print_r (hex_dump ($ info ));
- /*
- Output result:
- 0: 74 68 69 73 20 69 73 20 61 20 74 65 73 74 00 99 [this is a test ..]
- 10: 68 65 78 5f 64 75 6d 70 [hex_dump]
- */
- ?>