Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: dec to bin conversion for large numbers

by claree0 (Hermit)
on Sep 14, 2001 at 17:55 UTC ( [id://112422]=note: print w/replies, xml ) Need Help??


in reply to dec to bin conversion for large numbers

have a look at pack and unpack

Replies are listed 'Best First'.
Re: Re: dec to bin conversion for large numbers
by jlongino (Parson) on Sep 14, 2001 at 18:36 UTC
    If you have an example, I would be interested in seeing it. I've been playing around with How do I convert between decimal and binary? to help this person but keep running into the 32-bit limitation also. I tried modifying the example by vroom to no avail. Note that where vroom used "B32" I changed it to "B64". There may be a way to finesse it into working but I haven't lucked into it yet.
    print dec2bin(2**31); sub dec2bin { my $str = unpack("B64", pack("N", shift)); $str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros return $str; }
    This correctly produces
    10000000000000000000000000000000
    But if you use anything larger than 2**31, you get
    11111111111111111111111111111111
    @a=split??,'just lose the ego and get involved!';
    for(split??,'afqtw{|~'){print $a[ord($_)-97]}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://112422]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-29 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found