ruben has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem with the following code ,it does not give the required random binary value for a number greater than 32 .Is there a way to get it to work for atleast 64 bit binary answer ;and to convert this binary back to decimal. The perl I use does not have `Q'
use strict my($num2,$num1,$binary,$j ); print "enter the number\n"; $num2=rand 2**$j; $num1=int $num2; $binary = dec2bin($num1); print "binary=$binary\n"; sub dec2bin { my $str =join '', unpack "B32", pack "N", shift; $str=substr($str,-$j); return $str; }
thanx in advance, ruben

Update: lc $title. larsen

Replies are listed 'Best First'.
Re: dec2bin for more than 32 bits
by particle (Vicar) on May 16, 2002 at 11:38 UTC
Re: dec2bin for more than 32 bits
by jsprat (Curate) on May 16, 2002 at 17:19 UTC
    Bit::Vector is your friend, especially to_Bin.
    Here's an example, all error checking is up to you;)

    use Bit::Vector; $_ = '1234567891234567'; my $vec = Bit::Vector->new_Dec(64, $_); print $vec->to_Bin, "\n"; Output: 0000000000000100011000101101010100111100100110111010111100000111

    A side note: I had to quote the number to get Bit::Vector to accept it.
Re: Problem with integer to decimal conversion
by talexb (Chancellor) on May 16, 2002 at 13:19 UTC
    Hmm. This smells like homework.

    What problem are you trying to solve? Explain what steps you have taken and perhaps we can help you solve the problem. Your code fragment doesn't run (I suspect -- since there's no ';' after use strict) and you never read the user's input.

    Come back with more effort and we'll see what we can do for you.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

    ps Please, don't use all caps in the title to get our attention. It doesn't work. :)

      Your code fragment doesn't run (I suspect -- since there's no ';' after use strict) and you never read the user's input.

      Try running the "code fragment" with perl -s - -j=31 or perl -s - -j=33 and you may be in for a surprise :-)

      Okay, I am wearing my trickster hat, but I'm surprised you did not see the ';' after use strict ... the indentation alone should give it away. You gotta love the DWIM of Perl though. Even where it is less than intuitive :-)

      The Sidhekin
      print "Just another Perl ${\(trickster and hacker)},"

Re: Problem with integer to decimal conversion
by ruben (Novice) on May 17, 2002 at 04:58 UTC
    Sorry for the errors in the code.I am new to this.This is the corrected code
    use strict; my($num2,$num1, $binary,$j ); print "enter the number\n"; $j=<>; $num2=rand 2**$j; $num1=int $num2; $binary = dec2bin($num1); print "binary=$binary\n"; sub dec2bin { my $str =join '', unpack "B32", pack "N", shift; $str=substr($str,-$j); return $str; }
    regards,
    ruben
Re: Problem with integer to decimal conversion
by dvergin (Monsignor) on May 19, 2002 at 14:42 UTC
    As talexb says, it would be helpful if you told us why you are doing this.

    Because of the way you state the question, it seems that you may be under the impression that there is such a thing as a binary value as distinct from a decimal value. That is not the case. Inside Perl, a numeric value is just a numeric value.

    So what you are doing only applies to the display of a value or the representation of the value as a string consisting of a series of '1' characters and '0' characters -- which is different from actual binary bits.

    And that may be exactly what you need. But you don't tell us how this gets used, so it is hard to give you a more helpful response.