[Previous] [Contents] [Next]

Number Systems: binary, octal, and hexadecimal

PHP provides the following functions that convert numbers between integer decimal and the commonly used number systems, binary, octal, and hexadecimal:

	string decbin(integer number)
	integer bindec (string binarystring)
	string dechex(integer number)
	integer hexdec(string hexstring)
	string decoct(integer number)
	integer octdec(string octalstring)

The decimal numbers are always treated as integers, and the numbers in the other systems are treated as strings. When converting to decimal, care must be taken that the source number isn't greater than the maximum value an integer can hold. Here are some examples:

Decimal to binary

	echo decbin(45);
		// prints "101101"

Binary to decimal

	echo bindec("1001011");
		// prints 75

Decimal to hexadecimal

	echo dechex(45);
		// prints "2D"

Hexadecimal to decimal

	echo hexdec("5a7b");
		// prints 23163

Decimal to octal

	echo decoct(45);
		// prints "55"

Octal to decimal

	echo octdec("777");
		// prints 511

[Previous] [Contents] [Next]