On Mar 4, 2005, at 6:28 AM, Detlef Lindenthal wrote:

What would be the easiest way to display a number as dual / binary number?
For example: 29 is to be displayed as 11101.


In PHP this would work: <? printf ("%b", 29); ?>

but not so in Perl.
Any idea?

First, I would convert the number to a string of hex digits using sprintf("%x", $num). Then, do a regex substitution in eval-mode where each character x is used as a key into a hash whose value replaces it. The hash will contain:

'0' => '0000',
'1' => '0001',
...
'f' => '1111'

You can create the hash using a BEGIN clause so it's only done once. After that, no loops!

Josh

--
Joshua Juran
Metamage Software Creations - Mac Software and Consulting
http://www.metamage.com/

               * Creation at the highest state of the art *



Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to