RE: String format best practice

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> > I need to output a Hex value, e.g.:
> >
> > 686E8AF8
> >
> > In the following format:
> >
> > 68 6E 8A F8
> >
> > In other words, separated by spaces. Now I realize there 
> are a million 
> > ways to do this, but I would like a suggestion on which you 
> think is 
> > the most efficient? Incidentally, I'm starting with a decimal value 
> > (23602210718), so maybe sprintf would be a direct way?
> 
> sprintf and "most efficient" rarely belong in the same sentence. :-)
> 
> I believe this is going to "win" almost any efficiency test, 
> and is reasonably self-documenting/clear:
> 
> $dec = 23602210718;
> $hex = dechex($dec);
> echo implode(' ', chunk_split($hex, 2));

I presume you meant str_split()

And I think sprintf() is the way togo, it maybe slow, bit it'll be quicker than creating arrays, and rejoining.
Not that either method will make a huge difference unless your doing thousands of such operations.

	echo sprintf('%02x %02x %02x %02x', ($dec >> 24) & 0xff, ($dec >> 16) & 0xff, ($dec >> 8) & 0xff, $dec & 0xff);

Jared

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux