Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Padding hex strings with leading 0's

Status
Not open for further replies.

Guggly

Programmer
Jan 18, 2004
110
US
Hi! I'm converting a number into hex using the dechex function which works just fine. However, I need the output hex string to include leading zeros as a constant 8-character string. What can I use to do this? Thanks! -- Mike
 
I can think of a couple of ways off the top of my head.

One is using printf():

Code:
<?php
$foo = 125;

$bar = dechex($foo);

printf ('%08s', $bar);
?>


The other is using string concatenation and trimming:

Code:
<?php
$foo = 125;

$bar = '00000000' . dechex($foo);

print substr($bar, -8);
?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top