How PHP implements the transcoding and conversion code and function examples between several binaries

Source: Internet
Author: User
Tags binary to decimal decimal to binary function examples
one, decimal conversion

1, decimal to Binary decbin () function, the following example

Echo Decbin (12); Output 1100 echo Decbin (26); Output 11010

Decbin
(PHP 3, PHP 4, PHP 5)
Decbin--decimal conversion into binary
Description
string Decbin (int number)
Returns a string that contains a binary representation of the given number parameter. The maximum value that can be converted is a decimal of 4294967295, and the result is a string of 32 1.

2, decimal turn octal decoct () function

Echo decoct (15); Output of the Echo decoct (264); Output 410

Decoct
(PHP 3, PHP 4, PHP 5)
DECOCT--Decimal conversion to octal
Description
string decoct (int number)
Returns a string that contains the octal representation of the given number parameter. The maximum value that can be converted is a decimal of 4294967295, and the result is "37777777777".

3, decimal to hexadecimal dechex () function

Echo Dechex (10); Output a echo dechex (47); Output 2f

Dechex
(PHP 3, PHP 4, PHP 5)
Dechex--decimal conversion to hexadecimal
Description
string Dechex (int number)
Returns a string that contains the hexadecimal representation of the given number parameter. The maximum value that can be converted is 4294967295 in decimal and the result is "FFFFFFFF".

<?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 format for data conversion */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 $bytelen =ceil (16/$bin); Get if it is $bin, the length of a byte $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;//less than one byte length, Automatic front Supplement 0} $aOutChar []= $t; } return $aOutChar; }

Two, binary conversion

1, binary turn 16 into Bin2Hex () function

$binary = "11111001"; $hex = Dechex (Bindec ($binary)); echo $hex;//Output F9

Bin2Hex
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
Bin2Hex--converts binary data to hexadecimal notation
Description
String Bin2Hex (String str)
Returns an ASCII string that is the hexadecimal representation of the parameter str. The conversion uses byte mode, and the high four-bit byte takes precedence.

2, binary turn 10 into Bindec () function

echo bindec (' 110011 '); Output the Echo bindec (' 000110011 '); Output the Echo bindec (' 111 '); Output 7

Bindec
(PHP 3, PHP 4, PHP 5)
Bindec--Binary conversion to decimal
Description
Number Bindec (String binary_string)
Returns the decimal value of the binary number represented by the binary_string parameter.
Bindec () converts a binary number to an integer. The maximum number of convertible is 31 bits 1 or decimal 2147483647. Starting with PHP 4.1.0, the function can handle large values, in which case it returns a float type.

<?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 */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 ' = The ' 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; fo Reach ($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; }

Three, eight-binary conversion

Octal decimal octdec () function

Echo Octdec (' 77 '); Output Octdec Echo (DECOCT (45)); Output 45

Octdec
(PHP 3, PHP 4, PHP 5)
Octdec--Eight binary to decimal
Description
Number Octdec (String octal_string)
Returns the decimal equivalent of the octal number represented by the octal_string parameter. The maximum number of convertible values is 17777777777 or decimal 2147483647. PHP 4.1.0 begins, the function can handle large numbers, in which case it returns a float type.

Four, 16 binary conversions

hexadecimal to decimal hexdec () function

Var_dump (Hexdec ("See")); Var_dump (Hexdec ("ee")); Both print "int (238)" Var_dump (Hexdec ("that")); print "int" Var_dump (Hexdec ("A0")); print "int (160)"

Hexdec
(PHP 3, PHP 4, PHP 5)
Hexdec-16 conversion to decimal
Description
Number Hexdec (string hex_string)
return with Hex_st The decimal number equivalent to the hexadecimal number represented by the ring parameter. Hexdec () converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7FFFFFFF, which is 2147483647 of the decimal. PHP 4.1.0 begins, the function can handle large numbers, in which case it returns a float type.
Hexdec () replaces all non-hexadecimal characters encountered with 0. This way, all the left 0 are ignored, but the 0 on the right is counted into the value.

Related Article

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.