note
tye
<p>
The "q" format for pack()/unpack() is only supported if your copy of perl was compiled with quadword support (only available on some platforms).
</p><p>
But just about any version of Perl supports 53-bit integers which is often plenty, even when dealing with 64-bit data.
<code>
#!/usr/bin/perl -wl
use strict;
my $little;
BEGIN { $little= unpack "C", pack "S", 1; }
sub quad {
my( $str )= @_;
my $big;
if( ! eval { $big= unpack( "Q", $str ); 1; } ) {
my( $lo, $hi )= unpack "LL", $str;
( $hi, $lo )= ( $lo, $hi ) if ! $little;
$big= $lo + $hi*( 1 + ~0 );
if( $big+1 == $big ) {
warn "Forced to approximate!\n";
}
}
return $big;
}
my @quads= (
"\xff\xff\xff\xff\xff\xff\xff\xff",
"\x00\x3f\xff\xff\xff\xff\xff\xff",
"\x00\x1f\xff\xff\xff\xff\xff\xff" );
if( $little ) {
for( @quads ) {
$_= reverse $_;
}
}
for( @quads ) {
print quad( $_ );
}
__END__
This prints:
Forced to approximate!
1.84467440737096e+019
Forced to approximate!
1.8014398509482e+016
9.00719925474099e+015
</code></p><p>
Note that assumes an <em>unsigned</em> 64-bit integer.
</p>
-
<a href="/index.pl?node=tye&lastnode_id=1072">tye</a>
(but my friends call me "Tye")
36298
36298