Convert decimal to binary, octal, and hexadecimal digits to zero

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

<? Php Tutorial
/**
* Convert decimal to binary, octal, or hexadecimal with zero padding before the digits being insufficient *
*
* @ Param array $ datalist input data array (100,123,130)
* @ Param int $ bin conversion can be: 2, 8, 16
* @ Return array returns the data array () and returns the format in which no data is converted.
* @ Copyright chengmo qq: 8292669
*/
Function decto_bin ($ datalist, $ bin)
{
Static $ arr = array (, 'A', 'B', 'C', 'D', 'e ', 'F ');
If (! Is_array ($ datalist) $ datalist = array ($ datalist );
If ($ bin = 10) return $ datalist; // ignore in the same hexadecimal format
$ Bytelen = ceil (16/$ bin); // Obtain the length of a byte in $ bin notation.
$ Aoutchar = array ();
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; // The length of less than one byte is automatically increased to 0.
}
$ Aoutchar [] = $ t;
}
Return $ aoutchar;
}

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.