dechex

php128 apg


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



<?php

str dechex 
( int $number )


where,

$number = The decimal value to be converted to hexadecimal

?>
 

$number


The decimal value to convert to hexadecimal.


MINIMUM DECIMAL VALUE MAXIMUM DECIMAL VALUE bit
-4294967294 +4294967295 32
-9223372036854775806 +9223372036854775807 64
ed48

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



For the minimum and maximum values to convert under each architecture, see above tables.




  1 EXERCISE   

<?php

$nbr01_32m 
= -4294967294;

$nbr01_32M = 4294967295;

$nbr01_64m = -9223372036854775806;

$nbr01_64M = 9223372036854775807;

$nbr01_l = -1;

$nbr01_e = 1498;

$dechex_32m = dechex($nbr01_32m);

$dechex_32M = dechex($nbr01_32M);

$dechex_64m = dechex($nbr01_64m);

$dechex_64M = dechex($nbr01_64M);

$dechex_l = dechex($nbr01_l);

$dechex_e = dechex($nbr01_e);

echo 
'dechex( ' . $nbr01_32m . ' )=<br>= '  . 
$dechex_32m . '<br><br>';

echo 
'dechex( ' . $nbr01_32M . ' )=<br>= '  . 
$dechex_32M . '<br><br>';

echo 
'dechex( ' . $nbr01_64m . ' )=<br>= '  . 
$dechex_64m . '<br><br>';

echo 
'dechex( ' . $nbr01_64M . ' )=<br>= '  . 
$dechex_64M . '<br><br><br>';

echo 
'dechex( ' . $nbr01_l . ' )=<br>= '  . 
$dechex_l . '<br><br>';

echo 
'dechex( ' . $nbr01_e . ' )=<br>= '  . 
$dechex_e . '<br><br>';

?> 

 RESULT   

dechex( -4294967294 )=
= ffffffff00000002

dechex( 4294967295 )=
= ffffffff

dechex( -9223372036854775806 )=
= 8000000000000002

dechex( 9223372036854775807 )=
= 7fffffffffffffff


dechex( -1 )=
= ffffffffffffffff

dechex( 1498 )=
= 5da


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

  2 EXERCISE   

<?php

$nbr02a 
= -61.3245;

$nbr02b = 61.3245;

$dehexa = dechex($nbr02a);

$dehexb = dechex($nbr02b);

echo 
'FLOAT values:<br><br>';

echo 
'dechex( ' . $nbr02a . ' ) = ' . $dehexa . '<br><br>';

echo 
'dechex( ' . $nbr02b . ' ) = ' . $dehexb . '<br><br><br>';

echo 
'This is the same as INTEGER values:<br><br>';

$nbr02c = -61;

$nbr02d = 61;

$dehexc = dechex($nbr02a);

$dehexd = dechex($nbr02b);

echo 
'dechex( ' . $nbr02c . ' ) = ' . $dehexc . '<br><br>';

echo 
'dechex( ' . $nbr02d . ' ) = ' . $dehexd . '<br><br>';

?> 

 RESULT   

FLOAT values

dechex( -61.3245 ) = ffffffffffffffc3

dechex( 61.3245 ) = 3d



INTEGER values

dechex( -61 ) = ffffffffffffffc3

dechex( 61 ) = 3d


It does not matter if the value entered is of type FLOAT, because the treatment given by this function will always be of an INTEGER.

  3 EXERCISE   

<?php

if(PHP_VERSION < 8)
{
$nbr01_L = 9.199999E+19;
$dechex_L = dechex($nbr01_L);
echo 
'dechex( ' . $nbr01_L . ' )= '  . 
$dechex_L . '<br><br>';
}
else
{
echo 
'This FLOAT value is supported by this PHP.<br><br>';
$nbr01_L = 9.199999E+18;
$dechex_L = dechex($nbr01_L);
echo 
'dechex( ' . $nbr01_L . ' )= '  . 
$dechex_L . '<br><br>';
}

?> 

  4 EXERCISE   

<?php

$values 
= array(10,
                
3950.5,
                
3.9505e3,
                
03,
                
0x5F,
                
"10",
                
"3950.5",
                
"3.9505e3",
                
"039",
                
"0x5F",
                
true,
                
false,
                
null,
                );

foreach (
$values as $value) {
    try {
        echo 
$value . ' = ';
        echo 
'( '. dechex($value) . ')<sub>16</sub>';
        echo 
'<br><br>';
    } catch (
TypeError $exception) {
        echo 
$exception->getMessage() . "<br><br>";
    }
}

?>

  5 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);

$longVals = array(
    
MAX_64Bit, MIN_64Bit, MAX_32Bit, MIN_32Bit, 
    
MAX_64Bit - MAX_32Bit, MIN_64Bit - MIN_32Bit,
    
MAX_32Bit + 1, MIN_32Bit - 1, MAX_32Bit * 2, 
    (
MAX_32Bit * 2) + 1, (MAX_32Bit * 2) - 1,
    
MAX_64Bit -1, MAX_64Bit + 1, 
    
MIN_64Bit + 1, MIN_64Bit - 1
);


foreach (
$longVals as $longVal) {
    echo 
"Testing: $longVal<br>";
    try {
        
var_dump(dechex($longVal));
        echo 
'<br><br>';
    } catch (
TypeError $exception) {
        echo 
$exception->getMessage() . "<br><br>";
    }
}

?>

  6 EXERCISE   

<?php

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

echo "Testing dechex() : usage variations<br>";
//get an unset variable
$unset_var = 10;
unset (
$unset_var);

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

// get a class
class classA
{
}

// 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,

       
// object data
/*24*/ 
new classA(),

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

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

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

// loop through each element of $inputs 
// to check the behaviour of dechex()
foreach($inputs as $i => $input) {
    
$iterator = $i + 1;
    echo 
"<br>Iteration: $iterator<br>";
    try {
        
var_dump(dechex($input));
        echo 
'<br><br>';
    } catch (
TypeError $exception) {
        echo 
$exception->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 dechex() : usage variations<br>";
//get an unset variable
$unset_var = 10;
unset (
$unset_var);

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

// get a class
class classA
{
}

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

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

       
// 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,

       
// object data
/*24*/ 
new classA(),

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

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

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

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

?>