PHP implementation of the conversion to each other, PHP binary conversion _php Tutorial

Source: Internet
Author: User
Tags decimal to binary php language

PHP implements binary conversion, PHP conversion


From decimal to other binary conversions, the remainder is read by dividing the number by the number of binary numbers to be converted. Connected together on it.

<?php/** * Decimal to binary, octal, hexadecimal less than 0 * * * @param array $datalist incoming data array (100,123,130) * @param int $bin The conversion of the binary can be: 2, 8, * @return Array return data array () returns no data conversion format */function decto_bin ($datalist, $bin) {  static $arr =array ( 0,1,2,3,4,5,6,7,8,9, ' A ', ' B ', ' C ', ' D ', ' E ', ' F ');  if (!is_array ($datalist)) $datalist =array ($datalist);  if ($bin ==10) return $datalist; The same binary ignores  the $bytelen =ceil (16/$bin);//Gets the length of a byte  $aOutChar =array ()  If it is $bin-binary; foreach ($datalist as $num)  {    $t = "";    $num =intval ($num);  if ($num ===0) continue;    while ($num >0)    {      $t = $arr [$num% $bin]. $t;      $num =floor ($num/$bin);    }    $tlen =strlen ($t);    if ($tlen% $bytelen!=0)    {    $pad _len= $bytelen-$tlen% $bytelen;    $t =str_pad ("", $pad _len, "0", str_pad_left). $t; Less than one byte length, automatically preceded by 0    }    $aOutChar []= $t;  }  return $aOutChar;}

Test:

Var_dump (Decto_bin (Array (128,253), 2)), Var_dump (Decto_bin (Array (128,253), 8)), Var_dump (Decto_bin (Array (128,253), 16)); X-powered-by:php/5.2.0content-type:text/htmlarray (2) {[0]=> string (8) "10000000" [1]=> string (8) "11111101"} Array (2) {[0]=> string (4) "0200" [1]=> string (4) "0375"}array (2) {[0]=> string (2) "" [1]=> string (2) "FD"}

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:

<?php/** * Binary, octal, hexadecimal goto decimal * * * @param array $datalist incoming data array (DF,EF) * @param int $bin conversion can be: 2,8,16 * @return A Rray return Data Array () returns no data conversion format * @copyright Chengmo qq:8292669 */function bin_todec ($datalist, $bin) {  static $arr = Array (' 0 ' =>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); Splits a 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;  }  

Test:

Var_dump (Bin_todec (Array (' FF ', ' ff33 ', ' cc33 ')), Var_dump (Bin_todec (' 1101101 ', ' 111101101 '), 2); var_dump (Bin_todec (Array (' 1234123 ', ' 12341 '), 8)); X-powered-by:php/5.2.0content-type:text/htmlarray (3) {[0]=> int (255) [1]=> Int (65331) [2]=> int (52275)} Array (2) {[0]=> int (124) [1]=> Int (508)}array (2) {[0]=> int (342099) [1]=> int (5345)}

These are just ways to implement, in fact, do not care PHP language or other, the realization of ideas are the same. PHP actually has a few built-in functions to do this:

Bindec (), Decoct (), Dechex () Base_convert () Decbin () Here is just the idea. Oh!

Articles you may be interested in:

    • PHP implements a binary conversion (binary, octal, hex) to convert the implementation code to each other
    • PHP functions Description of decimal, binary, octal, and 16 binary conversion functions
    • The principle analysis of implementing Chinese character conversion in PHP
    • The implementation method of binary conversion of PHP images

http://www.bkjia.com/PHPjc/1119997.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119997.html techarticle PHP implements the conversion from one to the other, and the PHP binary converts from decimal to the other, using the number to be divided by the number of binary numbers to be converted, and the remainder to be read. Connect together to ...

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