How to convert int to hex with leading zeros in PHP

2 Answers

0 votes
$n = 253;
echo str_pad(dechex($n), 4, "0", STR_PAD_LEFT) . "\n";
 
$n = 10;
echo str_pad(dechex($n), 4, "0", STR_PAD_LEFT) . "\n";


  
  
  
/*
run:
   
00fd
000a
   
*/

 



answered Dec 27, 2021 by avibootz
0 votes
$n = 253;
echo sprintf("#%04X", $n) . "\n";
  
$n = 10;
echo sprintf("#%04X", $n);
 
 
   
   
   
/*
run:
    
#00FD
#000A
    
*/

 



answered Dec 29, 2021 by avibootz

Related questions

1 answer 225 views
1 answer 218 views
1 answer 219 views
1 answer 258 views
1 answer 226 views
1 answer 143 views
1 answer 194 views
...