PHP hexadecimal conversion

Source: Internet
Author: User
? Php/*** convert ASCII to hexadecimal and hexadecimal to ASCII * non-profit organizations or individuals may feel free to reprint them, for commercial purposes, please obtain the author's consent ** // Convert ASCII to hexadecimal functionasc2hex ($ str) {return '\ X '. substr (chunk_split (bin2hex ($ str), 2,

 

  1. /**
  2. *  
  3. * Convert ASCII to hexadecimal and hexadecimal to ASCII
  4. * For non-profit organizations or individuals, please feel free to repost them. For commercial purposes, please obtain the author's consent.
  5. *  
  6. */
  7. // Convert ASCII to hexadecimal
  8. Function asc2hex ($ str ){
  9. Return '\ X'. substr (chunk_split (bin2hex ($ str), 2,' \ X'), 0,-2 );
  10. }  
  11. // Convert hexadecimal to ASCII
  12. Function hex2asc ($ str ){
  13. $ Str = join ('', explode ('\ X', $ str ));
  14. $ Len = strlen ($ str );
  15. For ($ I = 0; $ I <$ len; $ I + = 2) $ data. = chr (hexdec (substr ($ str, $ I, 2 )));
  16. Return $ data;
  17. }  
  18. $ Asc = "haha ";
  19. $ Hex = asc2hex ($ asc );
  20. $ Asc = hex2asc ($ hex );
  21. Echo "hex:". $ hex;
  22. Echo"
  23. ";
  24. Echo "asc:". $ asc;
  25. ?>

 

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.