Php prints the hexadecimal string

Source: Internet
Author: User
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:

  1. /*
  2. Php prints the hexadecimal data of the string.
  3. */
  4. Function hex_dump ($ data, $ newline = "n ")
  5. {
  6. Static $ from = '';
  7. Static $ to = '';
  8. Static $ width = 16; # number of bytes per line
  9. Static $ pad = '.'; # padding for non-visible characters
  10. If ($ from = '')
  11. {
  12. For ($ I = 0; $ I <= 0xFF; $ I ++)
  13. {
  14. $ From. = chr ($ I );
  15. $ To. = ($ I >= 0x20 & $ I <= 0x7E )? Chr ($ I): $ pad;
  16. }
  17. }
  18. $ Hex = str_split (bin2hex ($ data), $ width * 2 );
  19. $ Chars = str_split (strtr ($ data, $ from, $ to), $ width );
  20. $ Offset = 0;
  21. Foreach ($ hex as $ I => $ line)
  22. {
  23. Echo sprintf ('% 6x', $ offset ). ':'. implode ('', str_split ($ line, 2 )). '['. $ chars [$ I]. ']'. $ newline;
  24. $ Offset + = $ width;
  25. }
  26. }
  27. $ Info = "this is a testx00x99hex_dump ";
  28. Print_r (hex_dump ($ info ));
  29. /*
  30. Output result:
  31. 0: 74 68 69 73 20 69 73 20 61 20 74 65 73 74 00 99 [this is a test ..]
  32. 10: 68 65 78 5f 64 75 6d 70 [hex_dump]
  33. */
  34. ?>

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.