PHP hexdec() Function


Definition and Usage

The hexdec() function returns a decimal number equivalent of a hexadecimal number embedded in a string.

This function returns a a decimal integer, though larger values may result in floats.

Syntax

hexdec ( string $hex_string ) : number

Parameters

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

Return Values

PHP hexdec() function returns a decimal number.

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 decimal equivalent of '100' and returns 256 −

<?php
   $arg='100';
   $val=hexdec($arg);
   echo "hexdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

hexdec(100) = 256

Example

 Live Demo

If the string contains invalid characters (other than 0-9 and a-f) they are ignored. Hence, in the string '2x5', 'x' is dropped and conversion of '25' is done which is 37. −

<?php
   $arg='2x5';
   $val=hexdec($arg);
   echo "hexdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

hexdec(2x5) = 37

Example

 Live Demo

If all character in string are non-hexadecimal, result is 0 −

<?php
   $arg='PHP';
   $val=hexdec($arg);
   echo "hexdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

hexdec(PHP) = 0

Example

 Live Demo

The hexdec() function treats the argument string contains unsigned integer and returns following result

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

Output

This will produce following result −

hexdec(-10) = 16

Updated on: 29-Jun-2020

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements