in reply to Re: Binary string to base64
in thread Binary string to base64

Thanks for the suggestion:

Working within Math::BigInt, it is easy to make the integer by prepending the string with '0b'. Then, between Math::Base85 and Math::BigInt, it is easy to go back and forth, though one needs to prepend '0's after the decoding to get back to the original length.

use Math::Base85 qw(from_base85 to_base85); $str = '0b000000010010001101000101011001111000100110101011110011011110 +1111'; $bint = Math::BigInt->new($str); $b85str = to_base85($bint); $bint2 = from_base85($b85str); print $bint2->as_bin();
This does exactly what I want now. And I get slightly more efficiency than with base64.

-albert

Replies are listed 'Best First'.
Re^3: Binary string to base64
by zentara (Cardinal) on Jun 16, 2006 at 21:21 UTC
    Hi, I was working on the same thing, but I added a '0b1' so that the leading zeroes in the binary wouldn't get truncated. It condenses it down to 10 characters. Here's my attempt:
    #!/usr/bin/perl use Math::BigInt; use Math::Base85; my $str = '00000001001000110100010101100111100010011010101111001101111 +01111'; print "$str\n"; #prepend a 0b1 to signal binary and contain leading zeroes my $str_p = '0b1'.$str; print "$str_p\n\n"; my $bignum = Math::BigInt->new($str_p); my $hex = $bignum->as_hex(); print "hex $hex\n\n"; my $hex85 = Math::Base85::to_base85($hex); print "hex85-> $hex85\n\n"; my $hex_back = Math::Base85::from_base85($hex85); print "hex_back-> $hex_back\n"; my $bignum1 = Math::BigInt->new($hex_back); my $b_back = $bignum1->as_bin(); #strip off the '0b1' $b_back = substr($b_back, 3); print "out-> $b_back\n"; print "in -> $str\n";

    I'm not really a human, but I play one on earth. flash japh