PHP dechex() Function


Definition and Usage

The dechex() function returns a string that contains hexadecimal equivalent of given decimal number argument.

This function returns a string with hexadecimal characters.

Syntax

dechex ( int $number ) : string

Parameters

Sr.NoParameter & Description
1number
A decimal number to be converted in equivalent hexadecimal representation

Return Values

PHP dechex() function returns a hexadecimal number inside string.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example calculates binary equivalent of 1001 and returns '3e9' −

<?php
   $arg=1001;
   $val=dechex($arg);
   echo "dechex(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

dechex(1001) = 3e9

Example

 Live Demo

Following example shows that fractional part of given number is ignored. Hence 100.55 is treated as 100 which is '64' in hexadecimal system. −

<?php
   $arg=100.55;
   $val=dechex($arg);
   echo "dechex(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

dechex(100.55) = 64

Example

 Live Demo

If string is provided as argument, result is 0 −

<?php
   $arg="Hello";
   $val=dechex($arg);
   echo "dechex(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

dechex(Hello) = 0

Example

 Live Demo

For negative decimal number, conversion is done using hexadecimal 2's complement method. Following example returns hexadecimal equivalent of -10

<?php
   $arg='-10';
   $val=dechex($arg);
   echo "dechex(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

dechex(-10) = fffffffffffffff6

Updated on: 29-Jun-2020

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements