Re: The 5.8.0 port anyone?

[email protected] (James Taylor) Wed, 2 Jul 2003 20:37:43 +0100 (BST)
Newsgroups perl.riscos
Message-ID <[email protected]>
On Wed 02 Jul, Glyn Royds wrote:
> 
> James Taylor <[email protected]> wrote:
> > 
> > What is %b016 supposed to do? Perhaps it would be better to write a
> > routine for Perl 5.005 to do it.
> 
> It converts a number to binary, padded out to 16 characters with 0's.
> As something of a perl novice I was about to start writing a routine
> to do the same

Well, here's one:

sub tobinary ($$) {
    my $mask = 1 << shift;
    my $value = shift;
    my $binary;
    $binary .= ($value & $mask) ? '1' : '0' while $mask >>= 1;
    return $binary;
}

Just pass it the wordsize and the integer value to convert.
Example:

print tobinary 16, 0x0AF5;

0000101011110101

Hope that helps.

-- 
James Taylor, Cheltenham, Gloucestershire, UK.       PGP key: 3FBE1BF9