in reply to Re^2: Add 1 to an arbitrary-length binary string
in thread Add 1 to an arbitrary-length binary string

Is there any way to turn a string of arbitrary length text into an arbitrary BigInt and back?

Not that I know of - though I don't usually use Math::BigInt.
Is it guaranteed that the "arbitrary length text" will contain only valid base 64 characters ?

Math::GMPz (plug) will certainly handle such text, even if it contains invalid base 64 characters - though Math::GMPz isn't included in perl, and depends upon the gmp C library:
use strict; use warnings; use Math::GMPz qw(:mpz); my ($order, $size, $endian, $nails) = (1, 1, 0, 0); my $s = 'Som3/Arbitr@ri!y/long/string'; my $z = Math::GMPz->new(); Rmpz_import($z, length($s), $order, $size, $endian, $nails, $s); print $z, "\n"; $z++; print "$z\n"; my $incremented_s = Rmpz_export( $order, $size, $endian, $nails, $z); print "$s\n"; print "$incremented_s\n"; # Next retrieve original string: $z--; my $orig_s = Rmpz_export( $order, $size, $endian, $nails, $z); print "$orig_s\n"; __END__ Outputs: 8786758437493627904416651010636024986357833879283932790893327248999 8786758437493627904416651010636024986357833879283932790893327249000 Som3/Arbitr@ri!y/long/string Som3/Arbitr@ri!y/long/strinh Som3/Arbitr@ri!y/long/string
But perhaps someone else can provide a solution that better suits your needs.

Cheers,
Rob.

Replies are listed 'Best First'.
Re^4: Add 1 to an arbitrary-length binary string
by einhverfr (Friar) on Nov 16, 2023 at 02:28 UTC
    This indeed does look correct. Thanks!