Php implements mutual conversion (binary, octal, and hexadecimal). code _ PHP Tutorial

Source: Internet
Author: User
Tags decimal to binary
Php implements binary conversion (binary, octal, and hexadecimal) to implement code conversion. Convert decimal to binary, octal, and hexadecimal to convert from decimal to other Hexadecimal. this number is used to divide the number by the number to be converted and read the remainder. The link can be converted to binary, octal, and hexadecimal in decimal format.
To convert from decimal to other hexadecimal values, the number is used to continuously divide by the number of hexadecimal values to be converted and read the remainder. Connect them together.

The code is as follows:


/**
* 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;
}


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.0
Content-type: text/html
Array (2 ){
[0] =>
String (8) "10000000"
[1] =>
String (8) "11111101"
}
Array (2 ){
[0] =>
String (4) 0200"
[1] =>
String (4) 0375"
}
Array (2 ){
[0] =>
String (2) "80"
[1] =>
String (2) "FD"
}
Binary, octal, and hexadecimal to decimal
This conversion uses multiplication, for example: 1101 to decimal: 1*2 ^ 3 + 1*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0
Code:

The code is as follows:


/**
* Convert binary, octal, and hexadecimal to decimal *
*
* @ Param array $ datalist input data array (df, ef)
* @ 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 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; // do not convert to the 10th hexadecimal format
$ AOutData = array (); // defines the output to save the array
Foreach ($ datalist as $ num)
{
$ Atnum = str_split ($ num); // splits 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:
Var_dump (bin_todec (array ('FF ', 'ff33', 'cc33'), 16 ));
Var_dump (bin_todec (array ('20140901', '20160901'), 2 ));
Var_dump (bin_todec (array ('20140901', '20160901'), 8 ));

X-Powered-By: PHP/5.2.0
Content-type: text/html
Array (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)
}
Later, these are just implementation methods. In fact, they do not care about php or other languages, and the implementation ideas are the same. Php actually has many built-in functions to accomplish this:
Bindec (), decoct (), dechex () base_convert () decbin () is just the implementation idea here. Haha!

The hexadecimal notation is used to convert data from decimal to other hexadecimal notation. this digit is used to continuously divide the number by the number of hexadecimal notation to be converted and read the remainder. Connect together...

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.