Php prints a string in hexadecimal format _ PHP Tutorial

Source: Internet
Author: User
Php prints the hexadecimal string. 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: Copy the code? The following function is a php hexadecimal instance that prints the 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]
*/
?>


Worker prints the hexadecimal instance of the string. The core function here is chr to obtain the binary value and then convert it to the hexadecimal number. The code is as follows ?...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.