PHP hex into binary code

Source: Internet
Author: User
    1. Var_dump (Decto_bin (Array (128,253), 2));

    2. Var_dump (Decto_bin (Array (128,253), 8));
    3. Var_dump (Decto_bin (Array (128,253), 16));

    4. x-powered-by:php/5.2.0

    5. Content-type:text/html

    6. Array (2) {

    7. [0]=>
    8. String (8) "10000000"
    9. [1]=>
    10. String (8) "11111101"
    11. }
    12. Array (2) {
    13. [0]=>
    14. String (4) "0200"
    15. [1]=>
    16. String (4) "0375"
    17. }
    18. Array (2) {
    19. [0]=>
    20. String (2) "80"
    21. [1]=>
    22. String (2) "FD"
    23. }

Copy Code

2, binary, octal, hexadecimal to decimal this conversion is multiplied, for example: 1101 to decimal: 1*2^3+1*2^2+0*2^1+1*2^0

Code:

     0, ' 1 ' =>1, ' 2 ' =>2, ' 3 ' =>3, ' 4 ' =>4, ' 5 ' =>5, ' 6 ' =>6, ' 7 ' =>7, ' 8 ' =>8, ' 9 ' =>9, ' A ' =>10, ' B ' =    >11, ' C ' =>12, ' D ' =>13, ' E ' =>14, ' F ' =>15);    if (!is_array ($datalist)) $datalist =array ($datalist); if ($bin ==10) return $datalist; For 10 binary does not convert $aOutData =array ();         Define output Save array foreach ($datalist as $num) {$atnum =str_split ($num);//Divide the string into a single character array $atlen =count ($atnum);        $total = 0;        $i = 1;                         foreach ($atnum as $tv) {$tv =strtoupper ($TV);                if (Array_key_exists ($TV, $arr)) {if ($arr [$tv]==0) continue;            $total = $total + $arr [$tv]*pow ($bin, $atlen-$i);        } $i + +;    } $aOutData []= $total; } return $aOutData;}? >

Test:

    1. Var_dump (Bin_todec (Array (' FF ', ' ff33 ', ' cc33 '), 16));

    2. Var_dump (Bin_todec (Array (' 1101101 ', ' 111101101 '), 2));
    3. Var_dump (Bin_todec (Array (' 1234123 ', ' 12341 '), 8));

    4. x-powered-by:php/5.2.0

    5. Content-type:text/html

    6. Array (3) {

    7. [0]=>
    8. Int (255)
    9. [1]=>
    10. Int (65331)
    11. [2]=>
    12. Int (52275)
    13. }
    14. Array (2) {
    15. [0]=>
    16. Int (124)
    17. [1]=>
    18. Int (508)
    19. }
    20. Array (2) {
    21. [0]=>
    22. Int (342099)
    23. [1]=>
    24. Int (5345)
    25. }

Copy Code

Summary: In PHP built in a number of functions can be completed in the conversion of the above, such as Bindec (), Decoct (), Dechex () Base_convert () Decbin () and so on. Through this article, bring us a realization of PHP in the conversion of the idea, I hope to be helpful to everyone.

  • 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.