Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Binary to Decimal for >2**32

Reply
Thread Tools

Binary to Decimal for >2**32

 
 
Graham Drabble
Guest
Posts: n/a
 
      11-14-2007
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?

--
Graham Drabble
http://www.drabble.me.uk/
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-14-2007
Graham Drabble wrote:
> I have been using a binary to decimal converter for number <2**16 for
> a while now [...] 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


> use Math::BigInt;

[...]
> Which works but seems very inelegant. Does anyone have a better
> suggestion?


Use a perl that is compiled with 64-bit support on a 64-bit machine.

jue


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-15-2007

Quoth "Jürgen Exner" <(E-Mail Removed)>:
> Graham Drabble wrote:
> > I have been using a binary to decimal converter for number <2**16 for
> > a while now [...] 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

>
> > use Math::BigInt;

> [...]
> > Which works but seems very inelegant. Does anyone have a better
> > suggestion?

>
> Use a perl that is compiled with 64-bit support on a 64-bit machine.


s/ on a 64-bit machine//;

~% perl -v

This is perl, v5.8.8 built for i386-freebsd-64int
<snip>

~% perl -le'print sprintf "%048b", 2**37'
000000000010000000000000000000000000000000000000

Ben

 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      11-15-2007
On 11/14/2007 02:45 PM, Graham Drabble wrote:
> 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?
>


Use the 'as_bin' method, e.g.:

my $num = new Math::BigInt 2**47 + 2**31 + 2**30;
print $num->as_bin, "\n";

The output will be prefixed with "0b" which you can remove.
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      11-15-2007
On 11/15/2007 10:40 AM, Graham Drabble wrote:
> [...]
> Anyone got any advice regarding a substitute for hex()? Whilst it works
> on all the environments I see I don't like to rely on something working
> when the docs say it won't and the error message will worry users.
> Unfortunately using 64bit Perl isn't an option.
>


my $num2 = Math::BigInt->new('0x8000c0000000');
print $num2, "\n";



I hope I made your day a little brighter.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decimal.Decimal formatting python@lists.fastmail.net Python 0 07-19-2010 12:32 PM
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM



Advertisments