# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use BigIntSupport ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
our %EXPORT_TAGS = ( 'all' => [ qw(
our @EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
# Preloaded methods go here.
$hexstring = 0 unless defined $hexstring;
$hexstring =~ s/^0[xX]//;
my $bignum = Math
::BigInt
->new(0);
while(length $hexstring) {
my $tail = substr $hexstring, -8, 8, "";
my $tailval = hex($tail);
$bignum += Math
::BigInt
->new($tailval) << $shift;
return sprintf("%x", $bignum);
my $low = $bignum & 0xffffffff;
$string = sprintf("%08x", $low) . $string;
$string = '0' if $string eq '';
if($format =~ /^\s*\%?(0)?(\d+)?[lL]*([xd])\s*/) {
my ($lead, $width, $base) = ($1, $2, $3);
$width = 0 unless defined $width;
$base = '' unless defined $base;
$string = bigint2hex
($bigint);
my $strlen = length($string);
my $diff = $width - $strlen;
my $char = defined($lead)?
'0' : ' ';
my $leading = $char x
$diff;
$string = "$leading$string";
croak
"Bad format string: $format";
# Below is stub documentation for your module. You better edit it!
BigIntSupport - Perl extension for interfacing between Math::BigInt values and text strings.
# Create Math::BigInt from an arbitrary-sized hex number
my $bigint1 = hex2bigint("0xdeadbeefcafebabe01234567");
my $bigint2 = hex2bigint("0xffffffff");
my $string = bigint2hex($bigint1);
print "Result is 0x$string\n";
This provides some interface functions for the Math::BigInt library,
which is part of the perl standard libary. Basically, Math::BigInt is
a module for arbitrary-sized integers. It overloads all the standard
math operators for use with Math::BigInt objects, but it does not
overload either the hex() function or sprintf "%x", so it is difficult
to use Math::BigInt objects for large hex numbers. This module is a
solution to that problem.
The following functions are exported:
=item hex2bigint($hex_string)
Interprets $hex_string as a hex number (whether or not a leading 0x is
present). Returns a Math::BigInt object representing the number. To
convert decimal numbers to Math::BigInt objects, use
Math::BigInt->new($decimal_string).
=item bigint2hex($bigint)
Formats the given Math::BigInt object as a hexidecimal string and
returns it. The string has no leading zeros or 0x.
=item bigint_sprintf($bigint, $format)
Takes a Math::BigInt and a printf-style format string and returns a
string with the appropriately-formatted string. The format string can
contain only %x or %d, and may optionally include a width
specification, which may include a leading zero. In other words,
"%016x" is legal but "The answer is %d" is not. A leading "%" is
Jeff Gibson, jgibson@afara.com
Math::BigInt(3), perl(1).