hexdec


php128 apg

RETURNS a STRING containing a DECIMAL representation of the given HEXADECIMAL number argument.



<?php

int
|float hexdec string $hex_string )


where,

$hex_string The hexadecimal string to convert to decimal

?>
 

$hex_string


The HEXADECIMAL value to convert to DECIMAL.



MAXIMUM HEXADECIMAL VALUE MAXIMUN DECIMAL VALUE bit
ffffffff 4294967295 32
7fffffffffffffff 9223372036854775807 64
ed48



This function will ignore any NON-HEXADECIMAL characters it encounters.




  1 EXERCISE   

<?php

$nbr01_32m 
'ffffffff00000002';

$nbr01_32M 'ffffffff';

$nbr01_64m '8000000000000002';

$nbr01_64M '7fffffffffffffff';

$nbr01_e '5DA';

$hexdec_32m hexdec($nbr01_32m);

$hexdec_32M hexdec($nbr01_32M);

$hexdec_64m hexdec($nbr01_64m);

$hexdec_64M hexdec($nbr01_64M);

$hexdec_e hexdec($nbr01_e);

echo 
'hexdec( ' $nbr01_32m ') =<br>= ' 
$hexdec_32m '<br><br>';

echo 
'hexdec( ' $nbr01_32M ') =<br>= ' 
$hexdec_32M '<br><br>';

echo 
'hexdec( ' $nbr01_64m ') =<br>= ' 
$hexdec_64m '<br><br>';

echo 
'hexdec( ' $nbr01_64M ') =<br>= ' 
$hexdec_64M '<br><br>';

echo 
'hexdec( ' $nbr01_e ') =<br>= ' 
$hexdec_e '<br><br>';

?> 

 RESULT   

hexdec( ffffffff00000002) =
= 1.8446744069415E+19

hexdec( ffffffff) =
= 4294967295

hexdec( 8000000000000002) =
= 9.2233720368548E+18

hexdec( 7fffffffffffffff) =
= 9223372036854775807

hexdec( 5DA) =
= 1498


You must be careful when using this function, especially when handling negative values.

  2 EXERCISE   

<?php

var_dump
(hexdec("012345"));
echo 
'<br><br>';
var_dump(hexdec("12345"));
echo 
'<br><br>';
var_dump(hexdec("q12345"));
echo 
'<br><br>';
var_dump(hexdec("12345+?!"));
echo 
'<br><br>';
var_dump(hexdec("12345q"));
echo 
'<br><br>';
var_dump((float)hexdec("1234500001"));
echo 
'<br><br>';
var_dump((float)hexdec("17fffffff"));
echo 
'<br><br>';

?>

  3 EXERCISE   

<?php

/*
if (PHP_INT_SIZE != 4) 
die("skip this test is for 32bit platform only");
*/

$values = array(0x123abc,
                
0x789DEF,
                
0x7FFFFFFF,
                
0x80000000,
                
'0x123abc',
                
'0x789DEF',
                
'0x7FFFFFFF',
                
'0x80000000',
                
'0x123XYZABC',
                
311015,
                
'311015',
                
31101.3,
                
31.1013e5,
                
011237,
                
'011237',
                
true,
                
false,
                
null);
for (
$i 0$i count($values); $i++) {
    
$res hexdec($values[$i]);
    
var_dump($res);
    echo 
'<br><br>';
}
?>

  4 EXERCISE   

<?php

if (PHP_INT_SIZE != 8
die(
"skip this test is for 64bit platform only");

define("MAX_64Bit"9223372036854775807);
define("MAX_32Bit"2147483647);
define("MIN_64Bit", -9223372036854775807 1);
define("MIN_32Bit", -2147483647 1);

$hexLongStrs = array(
   
'7'.str_repeat('f',15),
   
str_repeat('f',16),
   
'7'.str_repeat('f',7),
   
str_repeat('f',8),
   
'7'.str_repeat('f',16),
   
str_repeat('f',18),
   
'7'.str_repeat('f',8),
   
str_repeat('f',9)
);


foreach (
$hexLongStrs as $strVal) {
   echo 
"Testing: $strVal<br>";
   
var_dump(hexdec($strVal));
   echo 
'<br><br>';
}

?>

  5 EXERCISE   

<?php

if (PHP_INT_SIZE != 8
die(
"skip this test is for 64bit platform only");

echo 
"Testing hexdec() : basic functionality";

$values = array(0x123abc,
                
0x789DEF,
                
0x7FFFFFFF,
                
0x80000000,
                
'0x123abc',
                
'0x789DEF',
                
'0x7FFFFFFF',
                
'0x80000000',
                
'0x123XYZABC',
                
311015,
                
'311015',
                
31101.3,
                
31.1013e5,
                
011237,
                
'011237',
                
true,
                
false,
                
null);

foreach(
$values as $value) {
    echo 
"<br>hexdec $value<br>";
    
var_dump(hexdec($value));
    echo 
'<br><br>';
};

?>

  6 EXERCISE   

<?php

/*
if (PHP_INT_SIZE != 4) 
die("skip this test is for 32bit platform only");
*/

echo "Testing hexdec() : usage variations";
//get an unset variable
$unset_var 10;
unset (
$unset_var);

// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;

// get a resource variable
$fp fopen(__FILE__"r");

$inputs = array(
       
// int data
/*1*/  
0,
       
1,
       
12345,
       -
2345,
       
4294967295,  
       
// largest decimal
       
4294967296,

       
// float data
/*7*/  
10.5,
       -
10.5,
       
12.3456789000e10,
       
12.3456789000E-10,
       
.5,

       
// null data
/*12*/ 
NULL,
       
null,

       
// boolean data
/*14*/ 
true,
       
false,
       
TRUE,
       
FALSE,

       
// empty data
/*18*/ 
"",
       
'',
       array(),

       
// string data
/*21*/ 
"abcxyz",
       
'abcxyz',
       
$heredoc,

       
// undefined data
/*24*/ 
@$undefined_var,

       
// unset data
/*25*/ 
@$unset_var,

       
// resource variable
/*26*/ 
$fp
);

// loop through each element of $inputs 
// to check the behaviour of hexdec()
$iterator 1;
foreach(
$inputs as $input) {
    echo 
"<br>Iteration: $iterator<br><br>";
    try {
        
var_dump(hexdec($input));
    } catch (
TypeError $e) {
        echo 
$e->getMessage(), "<br><br>";
    }
    
$iterator++;
};
fclose($fp);
?>

  7 EXERCISE   

<?php

if (PHP_INT_SIZE != 8
die(
"skip this test is for 64bit platform only");

echo 
"Testing hexdec() : usage variations";
//get an unset variable
$unset_var 10;
unset (
$unset_var);

// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;

// get a resource variable
$fp fopen(__FILE__"r");

$inputs = array(
       
// int data
/*1*/  
0,
       
1,
       
12345,
       -
2345,
       
4294967295,  
       
// largest decimal
       
4294967296,

       
// float data
/*7*/  
10.5,
       -
10.5,
       
12.3456789000e10,
       
12.3456789000E-10,
       
.5,

       
// null data
/*12*/ 
NULL,
       
null,

       
// boolean data
/*14*/ 
true,
       
false,
       
TRUE,
       
FALSE,

       
// empty data
/*18*/ 
"",
       
'',
       array(),

       
// string data
/*21*/ 
"abcxyz",
       
'abcxyz',
       
$heredoc,

       
// undefined data
/*24*/ 
@$undefined_var,

       
// unset data
/*25*/ 
@$unset_var,

       
// resource variable
/*26*/ 
$fp
);

// loop through each element of $inputs 
// to check the behaviour of hexdec()
$iterator 1;
foreach(
$inputs as $input) {
    echo 
"<br><br>Iteration: $iterator<br>";
    try {
        
var_dump(hexdec($input));
        echo 
'<br>';
    } catch (
TypeError $e) {
        echo 
$e->getMessage(), "<br>";
    }
    
$iterator++;
};
fclose($fp);
?>