Conversion between PHP binaries and strings

Source: Internet
Author: User
Tags unpack

<?phpheader ("Content-type: text/html; charset=utf-8");     /***  Converts a string into a binary *  @param  type  $str *  @return  type*/function strtobin ($STR) {   &NBSP;&NBSP;//1. List each character      $arr  = preg_split ('/(? <!^) (?! $)/u ',  $str);     //2.unpack character     foreach ($arr  as &$ V) {         $temp  = unpack (' h* ',  $v);          $v  = base_convert ($temp [1], 16, 2);         unset ($temp);     }    return join ('   ', $arr);} /***  binary conversion to string *  @param  type  $str *  @return  type*/function bintostr ($str) {      $arr  = explode ('   ',  $str);     foreach ($arr  as & $v) {         $v  = pack ("H". Strlen (Base_convert ($v,  2, 16)),  Base_convert ($v,  2, 16));     }    return join (",  $arr);} Echo strtobin ("PHP two times Development: www.php2.cc");; echo  ' <br/> '; Echo bintostr ("1110000 1101000 1110000 111001001011101010001100  111001101010110010100001 111001011011110010000000 111001011000111110010001  111011111011110010011010 1110111 1110111 1110111 101110 1110000 1101000  1110000 110010 101110 1100011 1100011 ");


Conversion between PHP binaries and strings

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.