Example code of string and multi-hexadecimal conversion functions in PHP

Source: Internet
Author: User
The following small series will bring you an example code about the string and multi-hexadecimal conversion functions in PHP. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the conversion function.

/*** [Convert a string to (2, 8, and 16 hexadecimal) ASCII code] * @ param string $ str [string to be processed] * @ param boolean $ encode [convert string to ASCII | convert ASCII to string] * @ param string $ intType [2, 8, hexadecimal flag] * @ return string byte_str [processing result] * @ author alexander */function strtoascii ($ str, $ encode = true, $ intType = "2 ") {if ($ encode = true) {$ byte_array = str_split ($ str); foreach ($ byte_array as & $ value) {$ value = ord ($ value ); switch ($ intType) {case 16: $ valu E = sprintf ("% 02x", $ value); break; case 8: $ value = sprintf ("% 03o", $ value); break; default: $ value = sprintf ("% 08b", $ value); break ;}} unset ($ value); $ byte_str = implode ('', $ byte_array );} else {$ chunk_size = $ intType = 16? 2: ($ intType = 8? 3: 8); $ byte_array = chunk_split ($ str, $ chunk_size); $ byte_array = array_filter (explode ("\ r \ n", $ byte_array )); foreach ($ byte_array as & $ value) {$ fun_name = $ intType = 16? 'Hexdec ': ($ intType = 8? 'Octed': 'bindec '); $ value = $ fun_name ($ value); $ value = chr ($ value);} unset ($ value ); $ byte_str = implode ('', $ byte_array);} return $ byte_str ;}

Multi-base in PHP

PHP integer values can be expressed in decimal, hexadecimal, octal, or binary. optional symbols (-or +) can be added before ).

Binary: [+-]? 0b [01] +

Gossip: [+-]? 0 [1-7] +

Decimal: [+-]? [1-9] [0-9] * | 0

Hexadecimal: [+-]? [XX] [0-9a-fA-F] +

Multi-hexadecimal conversion functions:

The above is all the sample code for the string and multi-hexadecimal conversion functions in PHP provided by the editor. I hope you can support PHP ~

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.