Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Perl
Perl Misc
Binary to Decimal for >2**32
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Graham Drabble, post: 4846587"] Hi all, I have been using a binary to decimal converter for number <2**16 for a while now using $bin = sprintf("%016b",$dec) which works fine. I've now had to try and do the same conversion for numbers up to 2** 48 and have discovered that this does not work for numbers >=2**32 (everything >=2**32 returns the same as (2**32)-1 ). I'm currently getting round this with use strict; use warnings; use Math::BigInt; my $num = new Math::BigInt 2**47 + 2**31 + 2**30; if ($num >= 2**32){ my $upper = int($num / (2**32)); my $lower = $num % 2**32; $upper = sprintf ("%016b",$upper); $lower = sprintf ("%032b",$lower); print "$upper$lower\n"; } else { printf ("%048b\n",$num); } Which works but seems very inelegant. Does anyone have a better suggestion? Also hex() returns a warning when given a hex string >2**32 but returns the correct result. Is this something that can be relied on or do I need to find a work around for that as well? [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Perl
Perl Misc
Binary to Decimal for >2**32
Top