Decimal System Conversion Function description, decimalsystem_php tutorial

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

Decimal System Conversion function description, Decimalsystem


One, decimal system conversion function description
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 17
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".

two, binary (binary system) conversion function Description
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 51
echo bindec (' 000110011 '); Output 51
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.

three, octal (octal system) conversion function Description
Octal decimal octdec () function

Echo Octdec (' 77 '); Output 63
Echo Octdec (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, Hex (hexadecimal) Conversion function description
hexadecimal to decimal hexdec () function

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

Var_dump (Hexdec ("that")); print "int (10)"
Var_dump (Hexdec ("A0")); print "int (160)"
Hexdec
(PHP 3, PHP 4, PHP 5)
Hexdec--16 binary to Decimal
Description
Number Hexdec (String hex_string)
Returns the decimal number equivalent to the hexadecimal number represented by the hex_string 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.

Five, Arbitrary binary conversion Base_convert () function

$hexadecimal = ' A37334 ';
Echo Base_convert ($hexadecimal, 16, 2);//Output 101000110111001100110100
Base_convert
(PHP 3 >= 3.0.6, PHP 4, PHP 5)

Base_convert-Converts a number between arbitrary binaries
Description
String Base_convert (string number, int frombase, int tobase)
Returns a string that contains the representation of number in the Tobase binary. The binary of number itself is specified by Frombase. Both Frombase and tobase can only be between 2 and 36 (including 2 and 36). Numbers above the decimal are expressed in letters A-Z, for example a means that 10,b represents 11 and Z represents 35.

This is mainly the PHP conversion function to organize, easy to develop the search, the relevant specific function description please refer to the PHP manual. Please pay attention to the next installment of the text character coding Study Series.


JS or PHP code that converts a decimal number to a hexadecimal number

JS decimal to other binary code as follows var m = 10;document.write (m.tostring (2) + "
"); Display as 10,102 binary document.write (m.tostring (8) + "
"); Shown as 12 8 binary document.write (m.tostring (10) + "
"); Display as 100 binary document.write (m.tostring (16) + "
"); Shown as a, the hexadecimal PHP conversion function is as follows: Bindec ()-Binary conversion to decimal
Decbin ()-Decimal converts to binary
Dechex ()-Decimal conversion to hexadecimal
Decoct ()-Decimal conversion to octal
Hexdec ()-16 Conversion to decimal
Octdec ()-Eight conversion to decimal
Base_convert () – Convert numbers between arbitrary conversions The following instructions are available: one, decimal system conversion function description
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 17
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 turn hex 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". Binary (binary system) conversion function description
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--Will ... Remaining full text >>

The problem of the function of the binary conversion

Package Com.test.numsys;

Import java.util.*;

/**
* Numeric conversion decimal binary octal hexadecimal
*/
public class Numsys {

public static void Main (string[] args) {

Numsys ns = new Numsys ();
System.out.println ("decimal 100 is converted to binary:" + ns.decimaltobinary (100));//1100100

System.out.println ("binary 1100100 is converted to octal:" + ns.binarytooctal (1100100));//144

System.out.println ("Binary 10110000011101 conversion to 16 binary:" + ns.binarytohex ("10110000011101")); 2c1d

System.out.println ("decimal 100 is converted to octal:" + ns.) Decimaltooctal (100));

System.out.println ("decimal 15400 is converted to 16 binary:" + ns.) Decimaltohex (15400));
}

Conversion decimal converted to binary with no decimal part implemented
public long decimaltobinary (int a) {

Long binary = 0L;
int[] Binaryarr = new int[64];
int i = 0;
do {
Binaryarr[i] = a%2;
A = A/2;
i + +;
} while (A! = 0);

String s = "";
for (int j = binaryarr.length-1; j>=0; j--) {
S+=BINARYARR[J] + "";
}

binary = Integer.parseint (s);

return binary;
}

Binary conversion to octal
Public String Binarytooctal (long a) {
String octal = "";
String s = a + "";
char [] c_temp = (A + ""). ToCharArray ();

int temp = 3-c_temp.length%3;
for (int i = 0; i
s = "0" + s;
}

char[] C2 = S.tochararray ();
List list = new ArrayList ();

for (int i = 0; i < c2.length; i + = 3) {
String stemp = c ... Remaining full text >>

http://www.bkjia.com/PHPjc/900986.html www.bkjia.com true http://www.bkjia.com/PHPjc/900986.html techarticle Decimal System Conversion function description, Decimalsystem one, decimal system conversion function Description 1, decimal to Binary decbin () function, the following instance of Echo ...

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